注解 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) 为止。如果没有超类具有该类型的注释,则查询将指示当前类没有这样的注释。
注意,如果使用注释类型注释类以外的任何事物,此元注释类型都是无效的。还要注意,此元注释仅促成从超类继承注释;对已实现接口的注释无效。
注解 java.lang.annotation.Inherited 介绍的更多相关文章
- 元注解——java.lang.annotation.Target(1.8)
参考资料:https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html 普通注解’只能用来注解’代码’,而’元注 ...
- java.lang和java.lang.annotation中实现Annotation的类小结
加了注解,等于打上了某种标记,没加,则等于没有某种标记,以后,其他程序可以用反射来了解你的类上面有无何种标记,看你有什么标记,就去干相应的事.标记可以加在类,方法,字段,包上,方法的参数上. (1) ...
- API 注解 & Java API annotation
API 注解 & Java API annotation 注解 annotation
- java.lang.String 使用介绍
这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关的知识点,包括字符串常量池.StringBuffer.StringBuilder,以及equals和==的用法 ...
- 解决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 ...
- Java注解(一):介绍,作用,思想及优点
“注解优先于命令模式”-出自<Effective Java> Java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单.注解(Annotation) 为我们在代码中添加信息提供 ...
- java.lang包
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.特性——不用import 2.String String x = "abc"; < ...
- 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句
这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点: 1.反射 主要用于提取注解信息 2.自定义异常 主要是为了 ...
- java annotation使用介绍
还望支持个人博客站:http://www.enjoytoday.cn 介绍 Annotation的中文名字叫注解,开始与JDK 1.5,为了增强xml元数据和代码的耦合性的产物.注解本身并没有业务逻辑 ...
随机推荐
- go context
Context 使用原则 1.不要把Context放在结构体中,要以参数的方式传递 2.以Context作为参数的函数方法,应该把Context作为第一个参数,放在第一位. 3.给一个函数方法传递Co ...
- go tcp
TCP编程 1.客户端和服务器 2.服务端的处理流程 监听端口 接收客户端的链接 创建goroutine,处理该链接 3.客户端的处理流程 建立与服务端的链接 进行数据收发 关闭链接 服务端代码 pa ...
- GDB调试qemu-kvm
GDB调试qemu-kvm 前面几篇博文都是记录一些kvm相关包编译安装及使用,但都没深入去代码看看.看源码在配合上相关原理才能更好的理解kvm.但qemu-kvm的代码量很多,对我来讲直接看源码收获 ...
- Windows server 2008 R2远程桌面3389端口号修改
修改 Windows 服务器默认远程端口 https://help.aliyun.com/document_detail/51644.html?spm=5176.doc51644.6.784.4iAH ...
- windows系统下mysql-8.0.13-winx64(zip安装)
一.下载地址: http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.13-winx64.zip 二.安装: 1.解压: mysql根路 ...
- netty的数据通信之心跳检测
问题1:我们想实现客户端和服务端建立连接之后,5秒钟之后如果没有数据传输就关闭与客户端的连接. 解决办法:在服务端加上下面一条代码 ch.pipeline().addLast(new ReadTime ...
- System.Web.Optimization 合并压缩技术的使用
捆绑和压缩原理是:将多个css文件动态合并和压缩为一个css文件.多个js文件动态合并和压缩为一个js文件,如此达到减少浏览器对服务器资源文件的请求数量.缩小资源文件的尺寸来提高页面反应速度的目的.A ...
- C语言数据类型的转换(隐式转换)
算术运算符中的转换规则: double ←── float 高↑long↑unsigned↑int ←── char,short 低 注意: 图中横向箭头表示必须的转换,如两个float型数参加运算, ...
- 使用eclipse粗略统计代码代数【原】
- JMeter之Ramp-up Period(in seconds)说明(可同时并发)
Ramp-up Period(in seconds) [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行. ...