1. MergedAnnotations是什么
MergedAnnotations:合并了一个或多个注解的注解"集合"类
因为在JDK中提供获取注解的api过于简易,稍微复杂一点的场景就无法胜任
所以Spring提供了MergedAnnotations,使获取注解上更加的容易和"人性化"
例如:
- 无法获取注解上的注解
- 子类无法获取父类的注解
- 子类无法获取接口的注解
- 内部类无法获取外部类的注解
@Repository
class RepositoryClass1 {
}
class RepositorySubClass1 extends RepositoryClass1 {
}
@Test
public void jdk_invisible() throws Exception {
// 1. 无法获取注解上的注解
Component component = RepositoryClass1.class.getAnnotation(Component.class);
Assertions.assertThat(component).isNull();
// 2. 子类无法获取父类的注解
Repository repository = RepositorySubClass1.class.getAnnotation(Repository.class);
Assertions.assertThat(repository).isNull();
}
2. 如何创建MergedAnnotations对象
以下为创建MergedAnnotations最常用的两个方法 ( static )
static MergedAnnotations from(AnnotatedElement element)
static MergedAnnotations from(AnnotatedElement element, SearchStrategy searchStrategy)
方法1实际上是方法2的重载
from(element, SearchStrategy.DIRECT)
SearchStrategy可选值:
枚举值 | 含义 |
---|---|
SearchStrategy.DIRECT | 仅当前类 |
SearchStrategy.INHERITED_ANNOTATIONS | 当前类 + 父类 ( @Inherited注解修饰的注解 ) |
SearchStrategy.SUPERCLASS | 当前类 + 父类 |
SearchStrategy.TYPE_HIERARCHY | 当前类 + 父类 + 接口 |
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES | 当前类 + 父类 + 接口 + 外部类 ( 当前类是内部类时才有外部类 ) |
代码示例
@Repository
class RepositoryClass1 {
class InnerClass {
}
}
@ComponentScan("com.package1")
@ComponentScan("com.package2")
interface ComponentScanInterface1 {
}
class RepositorySubClass1 extends RepositoryClass1 implements ComponentScanInterface1 {
}
@Test
public void usage() throws Exception {
// 1. 可以获取注解上的注解
assertTrue(MergedAnnotations.from(RepositoryClass1.class).isPresent(Repository.class));
assertTrue(MergedAnnotations.from(RepositoryClass1.class).isPresent(Component.class));
// 2. 可以获取父类上的注解
assertTrue(
MergedAnnotations.from(RepositorySubClass1.class, SearchStrategy.SUPERCLASS)
.isPresent(Repository.class)
);
// 2.1 可以获取父类上注解的注解
assertTrue(
MergedAnnotations.from(RepositorySubClass1.class, SearchStrategy.SUPERCLASS)
.isPresent(Component.class)
);
// 3. 可以获取接口上的注解
assertTrue(
MergedAnnotations.from(RepositorySubClass1.class, SearchStrategy.TYPE_HIERARCHY)
.isPresent(ComponentScan.class)
);
// 4. 可以获取外部类的注解
assertTrue(
MergedAnnotations.from(RepositoryClass1.InnerClass.class, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)
.isPresent(Repository.class)
);
}
3. 常用的方法
boolean isPresent(Class<A> annotationType)
给定一个注解类,判断是否存在MergedAnnotation<A> get(Class<A> annotationType)
返回给定注解类包装的MergedAnnotation
(比当前类少一个s
)
MergedAnnotation
类似map,持有了annotation对象的所有属性,提供了各种方法去访问annotation的属性
=============================================================================================
4. 额外补充
SearchStrategy.INHERITED_ANNOTATIONS
的使用场景请查看