0.使用自定義注解前,先了解相關元注解
java.lang.annotation 提供了四種元注解,專門注解其他的注解(在自定義注解的時候,需要使用到元注解):
@Documented – 注解是否將包含在JavaDoc中
@Retention – 什么時候使用該注解
@Target – 注解用於什么地方
@Inherited – 是否允許子類繼承該注解
1.)@Retention – 定義該注解的生命周期
● RetentionPolicy.SOURCE : 在編譯階段丟棄。這些注解在編譯結束之后就不再有任何意義,所以它們不會寫入字節碼。@Override, @SuppressWarnings都屬於這類注解。
● RetentionPolicy.CLASS : 在類加載的時候丟棄。在字節碼文件的處理中有用。注解默認使用這種方式
● RetentionPolicy.RUNTIME : 始終不會丟棄,運行期也保留該注解,因此可以使用反射機制讀取該注解的信息。我們自定義的注解通常使用這種方式。
2.)Target – 表示該注解用於什么地方。默認值為任何元素,表示該注解用於什么地方。可用的ElementType 參數包括
● ElementType.CONSTRUCTOR: 用於描述構造器
● ElementType.FIELD: 成員變量、對象、屬性(包括enum實例)
● ElementType.LOCAL_VARIABLE: 用於描述局部變量
● ElementType.METHOD: 用於描述方法
● ElementType.PACKAGE: 用於描述包
● ElementType.PARAMETER: 用於描述參數
● ElementType.TYPE: 用於描述類、接口(包括注解類型) 或enum聲明
3.)@Documented – 一個簡單的Annotations 標記注解,表示是否將注解信息添加在java 文檔中。
4.)@Inherited – 定義該注釋和子類的關系
@Inherited 元注解是一個標記注解,@Inherited 闡述了某個被標注的類型是被繼承的。如果一個使用了@Inherited 修飾的annotation 類型被用於一個class,則這個annotation 將被用於該class 的子類。
1.自定義注解類編寫的一些規則
1. Annotation 型定義為@interface, 所有的Annotation 會自動繼承java.lang.Annotation這一接口,並且不能再去繼承別的類或是接口.
2. 參數成員只能用public 或默認(default) 這兩個訪問權修飾
3. 參數成員只能用基本類型byte、short、char、int、long、float、double、boolean八種基本數據類型和String、Enum、Class、annotations等數據類型,以及這一些類型的數組.
4. 要獲取類方法和字段的注解信息,必須通過Java的反射技術來獲取 Annotation 對象,因為你除此之外沒有別的獲取注解對象的方法
5. 注解也可以沒有定義成員,,不過這樣注解就沒啥用了
PS:自定義注解需要使用到元注解
2.實例
AnimalName.java
package com.xiaojian.basics.annotation; /** * 動物種類 */ import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME; @Target(FIELD) @Retention(RUNTIME) @Documented public @interface AnimalName { String value() default ""; }
AnimalColor.java
package com.xiaojian.basics.annotation; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** * 動物顏色 */ @Target(FIELD) @Retention(RUNTIME) @Documented public @interface AnimalColor { /** * 顏色枚舉 */ public enum Color{黑色,白色,灰色,雜色}; Color animalColor() default Color.白色; }
AnimalMaster.java
package com.xiaojian.basics.annotation; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** * 動物主人 */ @Target(FIELD) @Retention(RUNTIME) @Documented public @interface AnimalMaster { int id() default -1; String name() default ""; String address() default ""; }
AnimalInfoUtil.java
package com.xiaojian.basics.annotation; import java.lang.reflect.Field; /** * 返回動物信息 */ public class AnimalInfoUtil { public static void getInfo(Class<?> clazz){ StringBuilder strAnimalName = new StringBuilder( "動物種類:"); StringBuilder strAnimalColor = new StringBuilder( "動物顏色:"); StringBuilder strAnimalProvider = new StringBuilder( "主人信息:"); Field[] fields = clazz.getDeclaredFields(); for(Field field : fields){ if(field.isAnnotationPresent(AnimalName.class)){ AnimalName animalAnimal = (AnimalName)field.getAnnotation(AnimalName.class); strAnimalName.append(animalAnimal.value()); System.out.println(strAnimalName); } else if(field.isAnnotationPresent(AnimalColor.class)){ AnimalColor animalColor = (AnimalColor)field.getAnnotation(AnimalColor.class); strAnimalColor.append(animalColor.animalColor().toString()); System.out.println(strAnimalColor); } else if(field.isAnnotationPresent(AnimalMaster.class)){ AnimalMaster animalMaster = (AnimalMaster)field.getAnnotation(AnimalMaster.class); strAnimalProvider.append("[編號:" + animalMaster.id() + ",主人名字:" + animalMaster.name() + ",地址:" + animalMaster.address() + "]"); System.out.println(strAnimalProvider); } } } }
測試:
package com.xiaojian.basics.annotation; /** * 測試類 */ public class Test { public static void main(String[] args) { AnimalInfoUtil.getInfo(Animal.class); } }
返回:
動物種類:狗
動物顏色:黑色
主人信息:[編號:1,主人名字:小明,地址:浙江省杭州市XXXXXXXX]
【參考鏈接】
https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.html