@Retention:定义注解的保留策略
可选值
SOURCE:注解仅存在于源码中,在class字节码文件中不包含
CLASS:默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
RUNTIME:注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target:定义注解的作用目标
可选值
TYPE:接口、类、枚举、注解
FIELD:字段、枚举的常量
METHOD:方法
PARAMETER:方法参数
CONSTRUCTOR:构造函数
LOCAL_VARIABLE:局部变量
ANNOTATION_TYPE:注解
PACKAGE:包
@Document:说明该注解将被包含在javadoc中
@Inherited:说明子类可以继承父类中的该注解
实例
package com.xx; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.PARAMETER,ElementType.METHOD})//使用于方法参数,及方法上 @Retention(RetentionPolicy.RUNTIME)//会在class字节码文件中存在,在运行时可以通过反射获取到 @Documented //将被包含在javadoc中 public @interface TestAnno { /** * 描述 */ String desc(); /** * 类型 */ String type(); }