什么是元数据(MetaData)

先直接贴一个英文解释:

Metadata is simply data about data. It means it is a description and context of the data. It helps to organize, find and understand data。

上面介绍的大概意思是:元数据是关于数据的数据,元数据是数据的描述和上下文,它有助于组织,查找,理解和使用数据。

常用的元数据类型有:

  • 标题和说明;
  • 标签和类别;
  • 谁创造的,何时创造的;
  • 谁最后修改时间,什么时候修改;
  • 谁可以访问或更新。

下面举两个列子:

每次使用当今的相机拍照时,都会收集并保存一堆元数据:

  • 日期和时间
  • 文档名称
  • 相机设置
  • 地理位置等

这些数据就是元数据,使用这些数据可以更好的使用照片,比如使用日期和时间信息可以做照片时光机功能(百度网盘好像就有这个功能),使用地理位置信息可以知道你去过哪里。

再看一个列子。

对于一篇博客

每个博客文章都有标准的元数据字段,这些元数据包括:

  • 标题,
  • 作者,
  • 发布时间
  • 类别,
  • 标签。

使用这些元数据可以进行博客的搜索、文章的分类展示管理等。

更多的列子,请参考我的一篇翻译文章:什么是元数据。

好了,到这边你应该已经知道什么是元数据MetaData并了解元数据的作用和功能了。下面就来看看在Spring中元数据是指代啥。

Spring中的MeatData

从上面的类图中,我们看到Spring中和MetaData相关的顶层接口有两个:ClassMetadata和AnnotatedTypeMetadata

ClassMetadata

ClassMetadata,顾名思义,就是表示 Java 中类的元数据。那么类的元数据有哪些呢,打开ClassMetadata的源代码(代码就不贴了),大致有下面这些:

  • 类名;
  • 是否是注解;
  • 是否是接口;
  • 是否抽象类;
  • 父类;
  • 实现的接口等;

详细信息自己可以翻看下源代码,这边要抓住的重点就是要知道ClassMetadata表示的是一个类的元数据。可以和第一节中我举的两个列子类比下。

从上面的类图中可以看出,ClassMetadata有一个实现类是StandardClassMetadata,这个类是基于反射实现获取类元数据的,这个也是类名中“Standard”的含义。

查看源代码你可以发现这个类唯一的一个构造函数已经被标注@Deprecated了,所以这个类已经不建议直接使用了。

AnnotatedTypeMetadata

这个接口表示的是注解元素(AnnotatedElement)的元数据。那什么是注解元素呢?

我们常见的Class、Method、Constructor、Parameter等等都属于它的子类都属于注解元素。简单理解:只要能在上面标注注解的元素都属于这种元素。

public interface AnnotatedTypeMetadata {

    // 此元素是否标注有此注解,annotationName:注解全类名
boolean isAnnotated(String annotationName); //取得指定类型注解的所有的属性 - 值(k-v)
// annotationName:注解全类名
// classValuesAsString:若是true表示 Class用它的字符串的全类名来表示。这样可以避免Class被提前加载
@Nullable
Map<String, Object> getAnnotationAttributes(String annotationName);
@Nullable
Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString); // 支持重复注解
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName); @Nullable
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString); }

这个接口的继承树如下:

两个子接口相应的都提供了标准实现以及基于ASM的Visitor模式实现。

ASM 是一个通用的 Java 字节码操作和分析框架。它可以用于修改现有类或直接以二进制形式动态生成类。 ASM 虽然提供与其他 Java 字节码框架如 Javassist,CGLIB类似的功能,但是其设计与实现小而快,且性能足够高。

AnnotationMetadata

这是理解Spring注解编程的必备知识,它是ClassMetadataAnnotatedTypeMetadata的子接口,具有两者共同能力,并且新增了访问注解的相关方法。可以简单理解为它是对注解的抽象。

经常这么使用得到注解里面所有的属性值:

AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annoMetadata, annType);

public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata {

	//拿到当前类上所有的注解的全类名(注意是全类名)
Set<String> getAnnotationTypes();
// 拿到指定的注解类型
//annotationName:注解类型的全类名
Set<String> getMetaAnnotationTypes(String annotationName); // 是否包含指定注解 (annotationName:全类名)
boolean hasAnnotation(String annotationName);
//这个厉害了,用于判断注解类型自己是否被某个元注解类型所标注
//依赖于AnnotatedElementUtils#hasMetaAnnotationTypes
boolean hasMetaAnnotation(String metaAnnotationName); // 类里面只有有一个方法标注有指定注解,就返回true
//getDeclaredMethods获得所有方法, AnnotatedElementUtils.isAnnotated是否标注有指定注解
boolean hasAnnotatedMethods(String annotationName);
// 返回所有的标注有指定注解的方法元信息。注意返回的是MethodMetadata 原理基本同上
Set<MethodMetadata> getAnnotatedMethods(String annotationName);
}

MethodMetadata

方法的元数据接口

public interface MethodMetadata extends AnnotatedTypeMetadata {

	String getMethodName();
// Return the fully-qualified name of the class that declares this method.
String getDeclaringClassName(); // Return the fully-qualified name of this method's declared return type.
String getReturnTypeName(); boolean isAbstract();
boolean isStatic();
boolean isFinal();
boolean isOverridable();
}

这个接口有两个实现:

  • StandardMethodMetadata:基于反射的标准实现;
  • MethodMetadataReadingVisitor:基于ASM的实现的,继承自ASM``的org.springframework.asm.MethodVisitor采用Visitor的方式读取到元数据。

元数据,是框架设计中必须的一个概念,所有的流行框架里都能看到它的影子,包括且不限于Spring、SpringBoot、SpringCloud、MyBatis、Hibernate等。它能模糊掉具体的类型,能让数据输出变得统一,能解决Java抽象解决不了的问题,比如运用得最广的便是注解,因为它不能继承无法抽象,所以用元数据方式就可以完美行成统一的向上抽取让它变得与类型无关,也就是常说的模糊效果,这便是框架的核心设计思想。

不管是ClassMetadata还是AnnotatedTypeMetadata都会有基于反射和基于ASM的两种解决方案,他们能使用于不同的场景:

  • 标准反射:它依赖于Class,优点是实现简单,缺点是使用时必须把Class加载进来
  • ASM:无需提前加载Class入JVM,所有特别特别适用于形如Spring应用扫描的场景(扫描所有资源,但并不是加载所有进JVM/容器~)

MetadataReader

spring 对MetadataReader的描述为:Simple facade for accessing class metadata,as read by an ASM.大意是通过ASM读取class IO流资源组装访问元数据的门面接口

类关系图

MetadataReader接口方法

public interface MetadataReader {

    /**
* 返回class文件的IO资源引用
*/
Resource getResource(); /**
* 为基础class读取基本类元数据,返回基础类的元数据。
*/
ClassMetadata getClassMetadata(); /**
*为基础类读取完整的注释元数据,包括注释方法的元数据。返回基础类的完整注释元数据
*/
AnnotationMetadata getAnnotationMetadata();
}

MetadataReader接口提供三个方法:

  1. 返回class文件的IO资源引用
  2. 返回基础类的元数据
  3. 返回基础类的完整注释元数据

SimpleMetadataReader

final class SimpleMetadataReader implements MetadataReader {

    //class类IO流资源引用
private final Resource resource; //class类元数据
private final ClassMetadata classMetadata;
//class类完整注释元数据
private final AnnotationMetadata annotationMetadata; /**
* 构建函数,用于通过过ASM字节码操控框架读取class读取class资源流
*/
SimpleMetadataReader(Resource resource, @Nullable ClassLoader classLoader) throws IOException {
// 获取class类IO流
InputStream is = new BufferedInputStream(resource.getInputStream());
ClassReader classReader;
try {
//通过ASM字节码操控框架读取class
classReader = new ClassReader(is);
}
catch (IllegalArgumentException ex) {
}
finally {
is.close();
} //注解元数据读取访问者读取注解元数据
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
classReader.accept(visitor,ClassReader.SKIP_DEBUG);
//注解元数据
this.annotationMetadata = visitor;
//class元数据
this.classMetadata = visitor;
this.resource = resource;
} @Override
public Resource getResource() {
return this.resource;
} @Override
public ClassMetadata getClassMetadata() {
//返回当前类元数据
return this.classMetadata;
} @Override
public AnnotationMetadata getAnnotationMetadata() {
//返回当前类的注解元数据
return this.annotationMetadata;
} }

SimpleMetadataReader 为MetadataReader的默认实现,在创建SimpleMetadataReader通过ASM字节码操控框架读取class读取class资源流生成classMetadata与annotationMetadata

MetadataReaderFactory

MetadataReaderFactory接口 ,MetadataReader的工厂接口。

允许缓存每个MetadataReader的元数据集。

类关系图

MetadataReaderFactory接口方法

public interface MetadataReaderFactory {
/**
* 根据class名称创建MetadataReader
*/
MetadataReader getMetadataReader(String className) throws IOException; /**
* 根据class的Resource创建MetadataReader
*/
MetadataReader getMetadataReader(Resource resource) throws IOException; }

MetadataReaderFactory接口提供两个方法:

  1. 根据class名称生成MetadataReader
  2. 根据class的Resource生成MetadataReader

SimpleMetadataReaderFactory

public class SimpleMetadataReaderFactory implements MetadataReaderFactory {
// 资源加载器,此类根据路径将给定的path生成IO流资源
private final ResourceLoader resourceLoader;
@Override
public MetadataReader getMetadataReader(String className) throws IOException {
try {
//根据classname生成class对应的资源路径
String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(className) + ClassUtils.CLASS_FILE_SUFFIX;
//获取classname的IO流资源
Resource resource = this.resourceLoader.getResource(resourcePath);
//调用资源创建MetadataReader
return getMetadataReader(resource);
}
catch (FileNotFoundException ex) {
}
} /**
* 根据class资源创建MetadataReader 默认实现
*/
@Override
public MetadataReader getMetadataReader(Resource resource) throws IOException {
return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
} }

SimpleMetadataReaderFactory类为MetadataReaderFactory的简单实现,默认实现了MetadataReaderFactory的两个方法

  • 在getMetadataReader(String className) 方法中根据className创建class的Resource,然后调用getMetadataReader(Resource resource)
  • 在getMetadataReader(Resource resource) 方法中默认创建了SimpleMetadataReader

CachingMetadataReaderFactory

public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory {
// 默认的缓存大小
public static final int DEFAULT_CACHE_LIMIT = 256;
// 内存缓存列表,Resource-MetadataReader的映射缓存
@Nullable
private Map<Resource, MetadataReader> metadataReaderCache; @Override
public MetadataReader getMetadataReader(Resource resource) throws IOException {
if (this.metadataReaderCache instanceof ConcurrentMap) { MetadataReader metadataReader = this.metadataReaderCache.get(resource);
if (metadataReader == null) {
metadataReader = super.getMetadataReader(resource);
//缓存到本地缓存
this.metadataReaderCache.put(resource, metadataReader);
}
return metadataReader;
}
else if (this.metadataReaderCache != null) {
synchronized (this.metadataReaderCache) {
MetadataReader metadataReader = this.metadataReaderCache.get(resource);
if (metadataReader == null) {
metadataReader = super.getMetadataReader(resource);
//缓存到本地缓存 this.metadataReaderCache.put(resource, metadataReader);
}
return metadataReader;
}
}
else {
return super.getMetadataReader(resource);
}
}
}

CachingMetadataReaderFactory 类在SimpleMetadataReaderFactory的基础上增加了缓存功能,对Resource-MetadataReader的映射做了本地缓存

ConcurrentReferenceCachingMetadataReaderFactory

这个工厂和CachingMetadataReaderFactory 的功能一致,只是这个工厂内部的缓存支持并发。

一个简单的使用例子

讲了这么多,最后用一个简单的例子来结束这篇文章。

public static void main(String[] args) throws Exception {
ResourceLoader resourceLoader = new DefaultResourceLoader();
CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory(resourceLoader);
MetadataReader metadataReader = factory.getMetadataReader(SpringIOCTest.class.getName());
ClassMetadata classMetadata = metadataReader.getClassMetadata();
Method[] methods = ClassMetadata.class.getMethods();
for (Method method : methods) {
System.out.println(method.getName() + ":" + method.invoke(classMetadata));
}
}

参考

Spring 中的 MetaData 接口的更多相关文章

  1. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  2. Spring中Aware相关接口原理

    Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...

  3. spring中的aware接口

    1.实现了相应的aware接口,这个类就获取了相应的资源. 2.spring中有很多aware接口,包括applicationContextAware接口,和BeanNameAware接口. 实现了这 ...

  4. Spring中的InitializingBean接口的使用

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法. 测试,如下: imp ...

  5. spring中一些aware接口

    Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...

  6. 谈谈Spring中的BeanPostProcessor接口

    一.前言   这几天正在复习Spring的相关内容,在了解bean的生命周期的时候,发现其中涉及到一个特殊的接口--BeanPostProcessor接口.由于网上没有找到比较好的博客,所有最后花了好 ...

  7. Spring中的InitializingBean接口

    InitializingBean接口为bean提供了初始化方法的方式,它只有afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: impo ...

  8. MyBatis-Spring中间件逻辑分析(怎么把Mapper接口注册到Spring中)

    1.      文档介绍 1.1.      为什么要写这个文档 接触Spring和MyBatis也挺久的了,但是一直还停留在使用的层面上,导致很多时候光知道怎么用,而不知道其具体原理,这样就很难做一 ...

  9. Spring中好玩的注解和接口

    测试中: 一.unit中集中基本注解,是必须掌握的. @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意p ...

随机推荐

  1. ping、telnet

    ping用来检查网络是否通畅或者网络连接速度的命令 telnet是用来探测指定ip是否开放指定端口 Telnet 一 .关于telnet 对于Telnet的认识,不同的人持有不同的观点,可以把Teln ...

  2. XCTF-基础Android

    前期工作 查壳,无.打开看是普通的输入注册码然后验证. 逆向分析 这题太简单了,就不贴图了.看Manifest得知MainActivity是启动类,也就是点进去输入注册码那个类.密码不重要,验证成功后 ...

  3. HTML5 初学者一步一步攀爬 努力加油学习

    Html 5 Html:Hyper Text MarKup Language超文本标签语言 Html:网页的源码 浏览器:"解释和执行"HTMl源码的工具 Head 标签内的信息用 ...

  4. Prometheus—告警altermanger

    Prometheus-告警altermanger 1.告警altermanger装配 2.告警Mysql 3.Prometheus针对nodes告警规则配置 相关内容原文地址链接: 51CTO:wfw ...

  5. 2.二层常用技术-Portfast和BPDU Guard、BPDU Filter

    PortFast (生成树端口加速) 1.在交换机上使用portfast命令,可以防止出现由于STP的收敛时间太长,导致主机的DHCP请求超时,从而使主机不能接收到DHCP地址的问题. 确保有一台服务 ...

  6. 【Spring-Security】Re01 入门上手

    一.所需的组件 SpringBoot项目需要的POM依赖: <dependency> <groupId>org.springframework.boot</groupId ...

  7. 1150 Travelling Salesman Problem

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  8. spoj687 REPEATS - Repeats (后缀数组+rmq)

    A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...

  9. hdu4325 Flowers

    Problem Description As is known to all, the blooming time and duration varies between different kind ...

  10. hdu5360 Hiking

    Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, want ...