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

  1. package java.lang.annotation;
  2.  
  3. /**
  4. * Indicates that an annotation type is automatically inherited. If
  5. * an Inherited meta-annotation is present on an annotation type
  6. * declaration, and the user queries the annotation type on a class
  7. * declaration, and the class declaration has no annotation for this type,
  8. * then the class's superclass will automatically be queried for the
  9. * annotation type. This process will be repeated until an annotation for this
  10. * type is found, or the top of the class hierarchy (Object)
  11. * is reached. If no superclass has an annotation for this type, then
  12. * the query will indicate that the class in question has no such annotation.
  13. *
  14. * <p>Note that this meta-annotation type has no effect if the annotated
  15. * type is used to annotate anything other than a class. Note also
  16. * that this meta-annotation only causes annotations to be inherited
  17. * from superclasses; annotations on implemented interfaces have no
  18. * effect.
  19. *
  20. * @author Joshua Bloch
  21. * @since 1.5
  22. * @jls 9.6.3.3 @Inherited
  23. */
  24. @Documented
  25. @Retention(RetentionPolicy.RUNTIME)
  26. @Target(ElementType.ANNOTATION_TYPE)
  27. public @interface Inherited {
  28. }

注解的作用:

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

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @SpringBootConfiguration
  6. @EnableAutoConfiguration
  7. @ComponentScan(excludeFilters = {
  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10. public @interface SpringBootApplication {
  11.  
  12. // .....省略
  13.  
  14. }

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

  1. @SpringBootApplication
    @Service
  2. public class Person {
  3.  
  4. }

然后有个类Employee继承了Person

  1. public class Employee extends Person{
  2.  
  3. }

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

  1. @Test
  2. public void test1(){
  3.  
  4. Class clazz = Employee.class ;
  5. if(clazz.isAnnotationPresent(SpringBootApplication.class)){
  6. System.out.println("true");
  7. }
  8.  
  9. }

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

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

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

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

注解 java.lang.annotation.Inherited 介绍的更多相关文章

  1. 元注解——java.lang.annotation.Target(1.8)

    参考资料:https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html 普通注解’只能用来注解’代码’,而’元注 ...

  2. java.lang和java.lang.annotation中实现Annotation的类小结

    加了注解,等于打上了某种标记,没加,则等于没有某种标记,以后,其他程序可以用反射来了解你的类上面有无何种标记,看你有什么标记,就去干相应的事.标记可以加在类,方法,字段,包上,方法的参数上. (1)  ...

  3. API 注解 & Java API annotation

    API 注解 & Java API annotation 注解 annotation

  4. java.lang.String 使用介绍

    这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...

  5. 解决java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.robolectric.annotation.Config.application()

    Deleting the .gradle folder worked for me too. Odd. Guessing some high level caching going on somewh ...

  6. Java注解(一):介绍,作用,思想及优点

    “注解优先于命令模式”-出自<Effective Java> Java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单.注解(Annotation) 为我们在代码中添加信息提供 ...

  7. java.lang包

    作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.特性——不用import 2.String String x = "abc"; < ...

  8. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句

    这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点:            1.反射            主要用于提取注解信息            2.自定义异常  主要是为了 ...

  9. java annotation使用介绍

    还望支持个人博客站:http://www.enjoytoday.cn 介绍 Annotation的中文名字叫注解,开始与JDK 1.5,为了增强xml元数据和代码的耦合性的产物.注解本身并没有业务逻辑 ...

随机推荐

  1. httprouter使用pprof

    httprouter使用pprof 参考:https://github.com/feixiao/httpprof 性能分析参考:https://github.com/caibirdme/hand-to ...

  2. Vue computed属性

    computed vs methods 我们可以使用Vue中的method计算出学科的总分,最终得到的总数结果是相同的. 在上例的基础上,我们把computed区块中的totalMarks函数整体移到 ...

  3. MyBatis-Plugins 的创建流程与执行顺序

    一.插件的解析,所有插件都会被添加到 InterceptorChain 类中,用于后续处理 org.apache.ibatis.builder.xml.XMLConfigBuilder private ...

  4. Linux基础(一)系统api与库函数的关系

    1. 系统api与库函数的关系 man 2 open 1.1 open 1.2 read/write 实现cat功能 #include <stdio.h> #include <uni ...

  5. node的优缺点及应用场景

    Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎(V8引擎执行Javascript的速度非常快,性能非常好) 可以说node是运行在服务器端V8引擎上的Ja ...

  6. python里的input

    python2和python3的input是不同的 python3的input 对于python3,只有input,官方文档里是这样描述的 def input(*args, **kwargs): # ...

  7. WEBGIS网页崩溃问题分析

    加载某一地区的系统页面时,过了几十秒,页面空白.曾经捕获到是WMTS服务异常的问题.本人推测可能是底图服务停止,使得WMTS服务无法进行而抛出的异常. 为了证实自己的猜想,鄙人对一个正常的系统,修改为 ...

  8. C# MVC EF框架 用事务

    using System.Transactions; [HttpPost] public JsonResult Update(InfoModel list) { using (TransactionS ...

  9. 【bzoj 4764】弹飞大爷

    Description 自从WC退役以来,大爷是越来越懒惰了.为了帮助他活动筋骨,也是受到了弹飞绵羊一题的启发,机房的小伙伴们决定齐心合力构造一个下面这样的序列.这个序列共有N项,每项都代表了一个小伙 ...

  10. mysql信息函数

    mysql> SELECT CONNECTION_ID();  #当前连接的ID+-----------------+| CONNECTION_ID() |+-----------------+ ...