Spring番外篇-MergedAnnotations

1. MergedAnnotations是什么

MergedAnnotations:合并了一个或多个注解的注解"集合"类

因为在JDK中提供获取注解的api过于简易,稍微复杂一点的场景就无法胜任
所以Spring提供了MergedAnnotations,使获取注解上更加的容易和"人性化"

例如:

  1. 无法获取注解上的注解
  2. 子类无法获取父类的注解
  3. 子类无法获取接口的注解
  4. 内部类无法获取外部类的注解
    @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 )

  1. static MergedAnnotations from(AnnotatedElement element)
  2. 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. 常用的方法

  1. boolean isPresent(Class<A> annotationType) 给定一个注解类,判断是否存在
  2. MergedAnnotation<A> get(Class<A> annotationType) 返回给定注解类包装的 MergedAnnotation (比当前类少一个s)

MergedAnnotation
类似map,持有了annotation对象的所有属性,提供了各种方法去访问annotation的属性

=============================================================================================

4. 额外补充

SearchStrategy.INHERITED_ANNOTATIONS的使用场景请查看

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值