注解 java.lang.annotation.Inherited 介绍

在Spring Boot中大量使用了@Inherited注解。我们来了解一下这个注解的用法,注解的源码:

package java.lang.annotation;

/**
* Indicates that an annotation type is automatically inherited.  If
* an Inherited meta-annotation is present on an annotation type
* declaration, and the user queries the annotation type on a class
* declaration, and the class declaration has no annotation for this type,
* then the class's superclass will automatically be queried for the
* annotation type.  This process will be repeated until an annotation for this
* type is found, or the top of the class hierarchy (Object)
* is reached.  If no superclass has an annotation for this type, then
* the query will indicate that the class in question has no such annotation.
*
* <p>Note that this meta-annotation type has no effect if the annotated
* type is used to annotate anything other than a class.  Note also
* that this meta-annotation only causes annotations to be inherited
* from superclasses; annotations on implemented interfaces have no
* effect.
*
* @author  Joshua Bloch
* @since 1.5
* @jls 9.6.3.3 @Inherited
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

注解的作用:

当某个注解类在它的类上定义了@Inherited注解,例如SpringBoot中的 @SpringBootApplication注解,@SpringBootApplication注解类就定义了@Inherited注解,看下源码中的红色部分:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

  // .....省略

}

那么现在有一个我们自己开发的类使用了这个注解,例如:

@SpringBootApplication
@Service
public class Person {

}

然后有个类Employee继承了Person

public class Employee extends Person{

}

那么现在在判断Employee类上有没有@SpringBootApplication时,通过代码验证:

@Test
    public void test1(){
        
        Class clazz = Employee.class ;
        if(clazz.isAnnotationPresent(SpringBootApplication.class)){
            System.out.println("true");     
        }
        
    }

上面这个测试用例执行将输出true,也就是子类中能查找到@SpringBootApplication ,但同样,你用上述代码查找Employee类上是否有Spring的@Service注解时,会输出false,至此你应该明白@Inherited注解的用意了吧。

经过这样的分析,我们再来读一下JDK的文档,就会比较容易理解了,否则会觉的有些绕,下面列出 @interface注解的中文文档:

指示注释类型被自动继承。如果在注释类型声明中存在 Inherited 元注释,并且用户在某一类声明中查询该注释类型,同时该类声明中没有此类型的注释,则将在该类的超类中自动查询该注释类型。此过程会重复进行,直到找到此类型的注释或到达了该类层次结构的顶层 (Object) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。

注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配...
    尼尔君阅读 592评论 0赞 0
  • java自定义注解Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置...
    半路和尚怎么出家阅读 4,650评论 0赞 3
  • 注:都是在百度搜索整理的答案,如有侵权和错误,希告知更改。 一、哪些情况下的对象会被垃圾回收机制处理掉  当对象对...
    Jenchar阅读 3,351评论 3赞 2
  • 1、Annotation的工作原理: JDK5.0中提供了注解的功能,允许开发者定义和使用自己的注解类型。该功能由...
    siriusF阅读 360评论 0赞 0
  • 一、概念   Annotation(注解)就是Java提供了一种源程序中的元素关联任何信息和任何元数据(metad...
    从菜鸟到老菜鸟阅读 2,304评论 0赞 2

友情链接更多精彩内容