自定義注解會需要元注解
元注解
java中有四種元注解:@Retention、@Inherited、@Documented、@Target
@Retention
注解的保留位置(枚舉RetentionPolicy),RetentionPolicy可選值:
- SOURCE:注解僅存在於源碼中,在class字節碼文件中不包含
- CLASS:默認的保留策略,注解在class字節碼文件中存在,但運行時無法獲得
- RUNTIME:注解在class字節碼文件中存在,在運行時可以通過反射獲取到
@Inherited
聲明子類可以繼承此注解,如果一個類A使用此注解,則類A的子類也繼承此注解
@Documented
聲明注解能夠被javadoc等識別
@Target
用來聲明注解范圍(枚舉ElementType),ElementType可選值:
- TYPE:接口、類、枚舉、注解
- FIELD:字段、枚舉的常量
- METHOD:方法
- PARAMETER:方法參數
- CONSTRUCTOR:構造函數
- LOCAL_VARIABLE:局部變量
- ANNOTATION_TYPE:注解
- PACKAGE:包
自定義注解介紹
自定義注解使用場景
- 類屬性自動賦值。
- 驗證對象屬性完整性。
- 代替配置文件功能,像spring基於注解的配置。
- 可以生成文檔,像java代碼注釋中的@see,@param等
定義一個自定義注解(使用元注解,每個元注解都是可選,非必選):
注解中可以聲明成員方法,聲明的成員方法為最終的注解里面的參數,成員方法可以使用default關鍵字設置默認值。
新建一個自定義注解:
自定義注解,在定義@Target(value=ElementType.FIELD)
和@Retention (value=RetentionPolicy.RUNTIME)
類時可能會報錯。RetentionPolicy/ ElementType cannot be resolved to a variable
解決辦法(手動導入類路徑):
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Target({ElementType.FIELD,ElementType.METHOD}) @interface MyAnno{ public String name() default "zhangsan"; public String email() default "hello@example.com"; }
定義一個User類,來使用自定義注解:
class User{ @MyAnno(name = "zhang") private String name; @MyAnno(name = "zhang@example.com") private String email; @MyAnno(name = "sayHelloWorld") public String sayHello(){ return ""; } }
通過反射獲取注解信息
下面我們通過反射來演示下獲取屬性的注解信息和方法的注解信息:
Method[] methods = User.class.getMethods();//獲取User類的所有方法 //Field[] fields = User.class.getFields(); Field[] fields = User.class.getDeclaredFields();//獲取User類的所有屬性 /* getFields:只能訪問public屬性及繼承到的public屬性 getDeclaredFields:只能訪問到本類的屬性,不能訪問繼承到的屬性 getMethods:只能訪問public方法及繼承到的public方法 getDeclaredMethods:只能訪問到本類的方法,不能訪問繼承到的方法 getConstructors:只能訪問public構造函數及繼承到的public構造函數 getDeclaredConstructors:只能訪問到本類的構造函數,不能訪問繼承到的構造函數 */ for (Field field : fields) { MyAnno annotation = field.getAnnotation(MyAnno.class); if(annotation!=null){ System.out.println("property="+annotation.name()); } } for (Method method : methods) { MyAnno annotation = method.getAnnotation(MyAnno.class); if(annotation!=null){ System.out.println("sayHello="+annotation.name()); } }
輸出如下:
property=zhang
property=zhang@example.com
sayHello=sayHelloWorld