美文网首页
Java注解Annotation

Java注解Annotation

作者: whynotybb | 来源:发表于2019-07-16 17:07 被阅读0次

The common interface extended by all annotation types

所有注解类型都继承自这个普通的接口。

一个注解准确意义上来说,只不过是一种特殊的注释而已,如果没有解析它的代码,它可能连注释都不如。而解析一个类或者方法的注解往往有两种形式,一种是编译期直接的扫描,一种是运行期反射。反射的事情我们待会说,而编译器的扫描指的是编译器在对 java 代码编译字节码的过程中会检测到某个类或者方法被一些注解修饰,这时它就会对于这些注解进行某些处理。

元注解:

@Target:注解的作用目标

@Retention:注解的生命周期

@Documented:注解是否应当被包含在 JavaDoc 文档中

@Inherited:是否允许子类继承该注解

@Target源码

通过@Target(value={ElementType.TYPE})传值。

EleMentType源码

其中FIELD是指标注在字段上,METHOD方法上,TYPE是指标注在类上。

Retention源码 作用范围

CLASS:Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.  This is the default behavior.

RUNTIME:Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

SOURCE:Annotations are to be discarded by the compiler.

demo:

1,定义@Love

定义Annotation

2,使用注解

使用注解

3,解析注解

解析注解

相关文章

网友评论

      本文标题:Java注解Annotation

      本文链接:https://www.haomeiwen.com/subject/hhptlctx.html