在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) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。

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

注解 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. saltstack SLS 安装haproxy+nginx实例分析学习

    本文主要以实例的形式去熟悉sls的部署流程及相关模块的使用 文件下载:https://github.com/unixhot/saltbook-code 目录结构 [root@k8s_master sa ...

  2. nGrinder TestRunner DNS / sun.net.spi.nameservice.NameServiceDescriptor

    s ngrinder3.3控制台验证脚本报错 http://ngrinder.642.n7.nabble.com/ngrinder3-3-td1301.html 目前发现3.3版本在控制台校验脚本报错 ...

  3. MyBatis-获取 xxxMapper

    Main 方法,mybatis 版本为 3.5.0 使用 MapperProxyFactory 创建一个 MapperProxy 的代理对象 代理对象里面包含了 DefaultSqlSession(E ...

  4. 正则表达式基于JavaScript的入门详解

    关于正则表达式,和很多前辈聊起这个知识点时,他们的反馈都比聊其他技术谦逊,而和很多刚入门的程序员讨论时甚至会有觉得你看不起他. 的确,正则表达式从通常的应用来看,的确不难,比如电话,邮箱等验证.语法, ...

  5. HDU 1046(最短路径 **)

    题意是要在一个矩形点阵中求能从一点出发遍历所有点再回到起始点的最短路径长度. 不需要用到搜索什么的,可以走一个“梳子型”即可完成最短路径,而情况可以被分成如下两种: 一.矩形的长或宽中有偶数,则可以走 ...

  6. in和hasOwnProperty的区别

    两者都代表查看某个属性是不是对象自己的,返回布尔值 in判断的是对象的所有属性,包括对象实例及其原型的属性 hasOwnProperty则是判断对象实例的是否具有某个属性

  7. 产线nginx路径跳转问题

    问题描述 应用在客户产线环境部署时,要求只需要输入域名就可以直接访问到应用,而不用输入完整的应用访问路径. 项目架构 前端使用nginx作为反向代理和负载均衡,后端部署多个tomcat实例. Web应 ...

  8. maven多模块依赖源码调试

    Maven多模块项目中,通常存在摸个模块同时依赖其他多个基础模块的情况.在eclipse中使用run-jetty-run插件调试时,常常会出现找不到被依赖模块对应源码的错误提示.举个例子,模块A同时依 ...

  9. 我的长大app开发教程第三弹:实现四个子页面绑定RadioButton

    在开始之前先上一张图 在上一节中我们实现了底部Button,这一弹我们要实现点击四个按钮分别切换到不同页面,我们可以把页面分为两部分,顶部栏和中间内容部分,我们可以通过线性布局包裹两部分内容,顶部栏又 ...

  10. adb.exe已停止工作

    提示adb.exe错误,我电脑上没有安装豌豆荚,也没运行其它应用,最后发现是360杀毒软件导致的,进程中关掉360Mobile即可.