一,基本概念
Java 注解是JDK5.0引入的注釋機制,可以被使用在類,方法,參數等地方中,並且可以通過Java的反射機制獲取注解中的內容,
注解相當於標簽,可以標識方法,類或屬性具有某些特征,在編譯器生成的類文件時,可以被嵌入到字節碼中。另外用戶可以自
定義注解,完成定制化的開發,尤其是在利用springboot進行項目開發時,我們會經常使用注解管理spring容器的bean,從而大大
提高了開發的效率。
二,常用注解
在開發過程中,我們可以經常看到一些內置的注解:
@Override :用於校驗該方法是否是重載方法,如果不是重載方法,而且還是使用這個注解則會報錯。
@Deprecated :用於過時的用法,如果繼續使用,編譯器會給出警告
@SuppressWarnings :用於指示編譯器忽略注解中聲明的警告
在編寫自定義注解時,也會使用一些元注解:
1,@Retention:定義了注解的保留策略(RetentionPolicy)
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { /** * Returns the retention policy. * @return the retention policy */ RetentionPolicy value(); }
其中RetentionPolicy是一個枚舉類型,共有三種枚舉值
(1)CLASS:此注解的缺省行為,表明在程序運行期間,注解可以被編譯器保存在類文件中,但不會被虛擬機保留。
(2)RUNTIME:表明在程序運行期間,既可以被編譯器保存在類文件中,也被虛擬機保留,所以注解的內容可以通過反射機制讀取
(3)SOURCE:注解會被編譯器丟棄
2,@Target:定義了注解的作用目標
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { /** * Returns an array of the kinds of elements an annotation type * can be applied to. * @return an array of the kinds of elements an annotation type * can be applied to */ ElementType[] value(); }
其中ElementType是一個枚舉類型,並且表明@Target注解擁有的是枚舉類型的數組,可以被指定多個值。
(1)TYPE:允許作用在類,接口或者枚舉聲明上
(2)FIELD:允許作用在屬性字段上
(3)METHOD:允許作用在方法上
(4)PARAMETER:允許作用在參數上
(5)CONSTRUCTOR:允許作用在構造器上
(6)LOCAL_VARIABLE:允許作用在本地變量上
(7)ANNOTATION_TYPE:允許作用在注解類型上
(8)PACKAGE:允許作用在包上
JDK1.8之后,新增TYPE_PARAMETER和TYPE_USE兩個屬性
(9)TYPE_PARAMETER:允許作用在類型參數上
(10)TYPE_USE:允許作用在使用類型的地方上
3,@Documented 定義注解可以包含在javadoc中
4,@Inherited:表明注解可以被子類集成使用
三,自定義注解
1,使用關鍵字@interface進行自定義注解,注解內容可以指定注解屬性的類型,缺省值等
1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target({ElementType.METHOD, ElementType.FIELD}) 4 public @interface MyAnnotation { 5 6 int id() default 0; 7 8 String[] value(); 9 }
自定義的注解MyAnnotation包含id和value兩個屬性,其中屬性id的類型為int,且缺省值為0,屬性value的類型為String數組。
注意,在聲明屬性時,屬性名后跟的小括號一定要加上。@MyAnnotation表明可以被使用在方法或屬性字段上,並且被編譯器保存在類文件中,
一直駐留在JVM虛擬機中,所以可以通過反射訪問到注解中的內容。
2,使用方法
1 public class User { 2 3 @MyAnnotation(value = {"male", "female"}) 4 public void getUser(String name, int age) { 5 6 System.out.println("user: [" + name + "," + age + "]"); 7 } 8 }
@MyAnnotation被使用在getUser方法上,並且指定注解的vlue屬性值為male和female
3,利用反射獲取注解
1 public class MyAnnotationTest { 2 3 public static void main(String[] args) throws Exception { 4 5 6 User user = new User(); 7 //通過返回獲取實例 8 Class<User> userClass = User.class; 9 10 Method method = userClass.getMethod("getUser", String.class, int.class); 11 //利用反射調用方法 12 method.invoke(user, "Rose", 24); 13 14 //獲取方法上的MyAnnotation注解 15 if (method.isAnnotationPresent(MyAnnotation.class)) { 16 17 //獲取方法上的注解實例 18 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); 19 20 String[] value = annotation.value(); 21 22 for (String v : value) { 23 System.out.printf("%s ", v); 24 } 25 } else { 26 System.out.println("沒有應用MyAnnotation注解"); 27 } 28 29 System.out.println(); 30 31 //獲取方法上的所有注解 32 Annotation[] annotations = method.getAnnotations(); 33 for (Annotation annotation : annotations) { 34 System.out.println(annotation); 35 } 36 37 } 38 39 }
利用Java的反射機制獲取方法上的注解內容,通過Method類的getAnnotation方法可以獲取到指定的注解,getAnnotations方法可以獲取到方法上的所用注解。
運行結果:
四,小結
注解相當於標簽,利用反射機制可以獲取到注解中的內容,可以作用在類,方法,參數等地方,使其具有某些屬性,通過注解對程序進行標識來實現特定的處理,讓編寫的程序更加簡潔。