元注解是定义注解时用到的注解,共有@Retention @Target @Document @Inherited 四种;
1. @Retention - 保留策略 - java.lang.annotation.Retention
1.1 SOURCE - 仅在源码中存在,编译的时候被丢弃掉,如@overrade、@SuppressWarnings等
作用时间在写代码到打包之前,作用于拼写检查
1.2 CLASS - 编译时注解 - 代码被编译后会保存在CLASS字节码中,但是在运行时不会被找到
作用时间在写代码到打包之前+打包,这个注解适合做一些初始化代码的操作
1.3 RUNTIME - 运行时注解 - 代码编译后会保存在CLASS字节码文件中,并且在VM运行期间会被保留,所以在运行的时候会被 找到
作用在整个过程中,这个注解的运行周期最长,运用最多
2. @Target - 注解的作用域 - java.lang.annotation.Target
2.1 TYPE /** Class, interface (including annotation type), or enum declaration */
作用域: 类、接口(包含注解)、枚举
2.2 FIELD - /** Field declaration (includes enum constants) */
作用域: 字段、枚举常量
2.3 METHOD - /** Method declaration */
作用域: 方法
2.4 PARAMETER - /** Formal parameter declaration */
作用域: 方法参数
2.5 CONSTRUCTOR - /** Constructor declaration */
作用域: 构造函数
2.6 LOCAL_VARIABLE - /** Local variable declaration */
作用域: 用于描述局部变量
2.7 ANNOTATION_TYPE - /** Annotation type declaration */
作用域: 注解
2.8 PACKAGE - /** Package declaration */
作用域: 包
2.9 TYPE_PARAMETER - /** Type parameter declaration */ - 从jdk1.8之后支持
作用域:局部变量
2.10 TYPE_USE - /** Use of a type */ - 从jdk1.8之后支持
作用域:对类型的注解,如泛型的注解
3. @Documented - 如果生成API的时候这个注解会被记录在内
4. @Inherited - 允许子类继承父类的注解。