概述
Java在1.5版本引入注解Annotation,又稱Java標注,注解是一種語法元數據,可以被直接使用到源代碼中,類/方法/變量/參數/包名等都可以被注解。和Javadoc標簽不同,編譯器在生成class文件時候能夠保留注解代碼,同時,可能為了在程序運行過程中(run-time)可以使用注解,Java虛擬機會把注解保留,這樣就可以通過反射獲取注解Annotation的相關信息。
內置注解
其實我們平時會經常遇見注解,例如@Override、@Deprecated等等,這些都是JDK中內置的注解,先來看看Java內置的注解主要有哪些。
•作用于Java代碼的注解
?@Override 檢查某個方法是否是復寫的方法,如果這個方法在父類或者實現的接口中未找到,編譯會出錯。
?@Deprecated 標記某個方法或者類被廢棄,如果使用該類或者方法,編譯過程會報警告
?@SuppressWarnings 通知編譯器忽略關于被標注的參數的警告
?@SafeVarargs 忽略關于調用含有泛型參數的方法或者構造器的警告,1.7新增注解
?@FunctionalInterface 表明某一個聲明的接口將被用作功能性接口,1.8新增注解
•作于其他注解的注解,被稱為元注解(Meta Annotation)
?@Retention 指明被標注的注解在什么時候使用(也就是注解什么時候會被保留)
■僅僅在源代碼中保留,在編譯過程中丟棄(RetentionPolicy.RUNTIME)
■注解在編譯過程中保存到class文件,在class文件被加載時候忽略(RetentionPolicy.CLASS)
■注解在class文件加載時候被讀取,也就是運行中注解可用,可以通過反射獲取注解信息(RetentionPolicy.RUNTIME)
?@Documented 指明在生成Javadoc時候,被標注的注解將被寫入Javadoc文檔中
?@Target 指明被標注的注解的作用范圍
■ElementType.TYPE:用于描述類、接口(包括注解類型) 或enum聲明
■ElementType.FIELD:用于描述域
■ElementType.METHOD:用于描述方法
■ElementType.PARAMETER:用于描述參數
■ElementType.CONSTRUCTOR:用于描述構造器
■ElementType.LOCAL_VARIABLE:用于描述局部變量
■ElementType.ANNOTATION_TYPE:用于描述注解
■ElementType.PACKAGE:用于描述包
?@Inherited 指明被標注的注解是被繼承的,也就是說如果一個@Inherited修飾的annotation類型被用于一個類,則這個annotation也會作用于改類的子類。
?@Repeatable 指明被標注的注解可以多次作用于同一個對象,1.9新增注解
自定義注解
上面說了那么多注解,大家集中關注元注解,我們自定義注解時候,通常會使用元注解來協助我們。自定義注解格式為public @interface 注解名 {定義體},使用@interface自定義注解時,自動繼承了java.lang.annotation.Annotation接口。自定義注解時,不能繼承其他的注解或接口。注解中聲明的方法實際上是聲明了一個注解參數,方法的名稱就是參數的名稱,返回值類型就是參數的類型,可以通過default來聲明參數的默認值。
自定義注解很簡單,使用@interface來定義一個注解,如下。
1
2
3
4
5
6
7
8
|
@Retention (RetentionPolicy.RUNTIME) @Target (ElementType.TYPE) @Documented public @interface ClassInfo { String author() default "Wang" ; String date(); String comments(); } |
自定義了一個名為ClassInfo的注解,根據@Retention可以得知這個注解會一直存在,也就是在程序運行中時候,這個注解還是有效的;@Target(ElementType.TYPE)說明ClassInfo注解的是作用于類、接口或enum聲明的;@Documented
說明ClassInfo信息可以被寫入Javadoc文檔中。
再來看一下自定義注解中的一些注解參數,里面有三個注解參數,注解參數是可以設置默認值,例如author注解參數,默認值為Wang,其他兩個參數就沒有默認值。
再來看另一個自定義的注解。
1
2
3
4
5
6
|
@Retention (RetentionPolicy.RUNTIME) @Target (ElementType.METHOD) public @interface MethodInfo { String description() default "No Description" ; String date(); } |
這個自定義的注解MethodInfo是作用于方法的,在程序運行中時候,這個注解也會存在;里面有兩個注解參數。
注解參數的定義(方法的定義),只能用public或者default兩個訪問權限修飾符,參數的類型支持以下幾種。
•八種基本數據類型(byte,int,short,long,float,double,char,boolean)
•String類型
•Class類型
•enum類型
•Annotation類型
•以上所有類型的數組
注解的使用
除了上面兩個注解,又添加了一個Field作用域的注解。
1
2
3
4
5
6
|
@Retention (RetentionPolicy.RUNTIME) @Target (ElementType.FIELD) public @interface FieldInfo { String type(); String name(); } |
自定義的注解中的注解參數如果沒有聲明默認值,在使用自定義注解時候,必須給這些參數賦值,否則編譯器會報錯。
看一下注解使用的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@ClassInfo (author = "wang" , date = "2016/9/13" , comments = "annotation demo" ) public class AnnotationDemo { @FieldInfo (type = "public" , name = "firstField" ) public int firstField; @FieldInfo (type = "private" , name = "secondField" ) private String secondField; @MethodInfo (description = "method in AnnotationDemo" , name = "firstMethod" ) public void firstMethod(String value) { System.out.printf( "first method involved" ); } @MethodInfo (description = "method in AnnotationDemo" , name= "secondMethod" ) private void secondMethod() { System.out.printf( "first method involved" ); } } |
獲取注解信息
要獲取注解信息,首先得保證注解在程序運行時候會存在,所以一般會給自定義的注解添加了@Retention(RetentionPolicy.RUNTIME)元注解,這樣在程序運行過程中,我們可以通過反射去獲取一些注解信息,關于反射的說明,可以查看這篇文章。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class AnnotationTest { public static void main(String[] args) { resolveClassAnnotationInfo(AnnotationDemo. class ); resolveFieldAnnotationInfo(AnnotationDemo. class ); resolveMethodAnnotationInfo(AnnotationDemo. class ); } private static void resolveClassAnnotationInfo(Class<?> clz) { // 判斷該類是否有ClassInfo注解 if (clz.isAnnotationPresent(ClassInfo. class )) { ClassInfo classInfo = (ClassInfo) clz.getAnnotation(ClassInfo. class ); System.out.println(classInfo.author() + " " + classInfo.comments() + " " + classInfo.date()); } } private static void resolveFieldAnnotationInfo(Class<?> clz) { Field[] fields = clz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(FieldInfo. class )) { FieldInfo fieldInfo = (FieldInfo) field.getAnnotation(FieldInfo. class ); System.out.println(fieldInfo.type() + " " + fieldInfo.name()); } } } private static void resolveMethodAnnotationInfo(Class<?> clz) { Method[] methods = clz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(MethodInfo. class )) { MethodInfo methodInfo = (MethodInfo) method.getAnnotation(MethodInfo. class ); System.out.println(methodInfo.name() + " " + methodInfo.description()); } } } } |
通過反射獲取類中的Field/Method等等,通過getAnnotation()或者getAnnotations()獲取相關注解,拿到具體注解就可以獲取具體的信息了。
運行結果輸出如下:

圖-1 運行結果圖
總結
對于Java的初學者甚至是有一定經驗的Java開發人員,對Java注解的接觸可能比較少,而在實際中,也很少用到注解,但是會經常會在代碼里面看見,這篇文章算是對注解的淺顯介紹,最起碼在代碼層面是閱讀無壓力的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。