Java注解提供了關于代碼的一些信息,但并不直接作用于它所注解的代碼內容。在這個教程當中,我們將學習Java的注解,如何定制注解,注解的使用以及如何通過反射解析注解。
Java1.5引入了注解,當前許多java框架中大量使用注解,如hibernate、Jersey、spring。注解作為程序的元數據嵌入到程序當中。注解可以被一些解析工具或者是編譯工具進行解析。我們也可以聲明注解在編譯過程或執行時產生作用。
在使用注解之前,程序源數據只是通過java注釋和javadoc,但是注解提供的功能要遠遠超過這些。注解不僅包含了元數據,它還可以作用于程序運行過程中、注解解釋器可以通過注解決定程序的執行順序。例如,在Jersey webservice 我們為方法添加URI字符串的形式的**PATH**注解,那么在程序運行過程中jerser解釋程序將決定該方法去調用所給的URI。
創建Java自定義注解
創建自定義注解和創建一個接口相似,但是注解的interface關鍵字需要以@符號開頭。我們可以為注解聲明方法。我們先來看看注解的例子,然后我們將討論他的一些特性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.journaldev.annotations; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target (ElementType.METHOD) @Inherited @Retention (RetentionPolicy.RUNTIME) public @interface MethodInfo{ String author() default 'Pankaj' ; String date(); int revision() default 1 ; String comments(); } |
- 注解方法不能帶有參數;
- 注解方法返回值類型限定為:基本類型、String、Enums、Annotation或者是這些類型的數組;
- 注解方法可以有默認值;
- 注解本身能夠包含元注解,元注解被用來注解其它注解。
這里有四種類型的元注解:
1. @Documented —— 指明擁有這個注解的元素可以被javadoc此類的工具文檔化。這種類型應該用于注解那些影響客戶使用帶注釋的元素聲明的類型。如果一種聲明使用Documented進行注解,這種類型的注解被作為被標注的程序成員的公共API。
2. @Target——指明該類型的注解可以注解的程序元素的范圍。該元注解的取值可以為TYPE,METHOD,CONSTRUCTOR,FIELD等。如果Target元注解沒有出現,那么定義的注解可以應用于程序的任何元素。
3. @Inherited——指明該注解類型被自動繼承。如果用戶在當前類中查詢這個元注解類型并且當前類的聲明中不包含這個元注解類型,那么也將自動查詢當前類的父類是否存在Inherited元注解,這個動作將被重復執行知道這個標注類型被找到,或者是查詢到頂層的父類。
4.@Retention——指明了該Annotation被保留的時間長短。RetentionPolicy取值為SOURCE,CLASS,RUNTIME。
Java內建注解
Java提供了三種內建注解。
1. @Override——當我們想要復寫父類中的方法時,我們需要使用該注解去告知編譯器我們想要復寫這個方法。這樣一來當父類中的方法移除或者發生更改時編譯器將提示錯誤信息。
2. @Deprecated——當我們希望編譯器知道某一方法不建議使用時,我們應該使用這個注解。Java在javadoc 中推薦使用該注解,我們應該提供為什么該方法不推薦使用以及替代的方法。
3. @SuppressWarnings——這個僅僅是告訴編譯器忽略特定的警告信息,例如在泛型中使用原生數據類型。它的保留策略是SOURCE(譯者注:在源文件中有效)并且被編譯器丟棄。
我們來看一個java內建注解的例子參照上邊提到的自定義注解。
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
|
package com.journaldev.annotations; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; public class AnnotationExample { public static void main(String[] args) { } @Override @MethodInfo (author = 'Pankaj' , comments = 'Main method' , date = 'Nov 17 2012' , revision = 1 ) public String toString() { return 'Overriden toString method' ; } @Deprecated @MethodInfo (comments = 'deprecated method' , date = 'Nov 17 2012' ) public static void oldMethod() { System.out.println( 'old method, don' t use it.'); } @SuppressWarnings ({ 'unchecked' , 'deprecation' }) @MethodInfo (author = 'Pankaj' , comments = 'Main method' , date = 'Nov 17 2012' , revision = 10 ) public static void genericsTest() throws FileNotFoundException { List l = new ArrayList(); l.add( 'abc' ); oldMethod(); } } |
相信這個例子可以不言自明并能展示在不同場景下的應用。
Java注解解析
我們將使用反射技術來解析java類的注解。那么注解的RetentionPolicy應該設置為RUNTIME否則java類的注解信息在執行過程中將不可用那么我們也不能從中得到任何和注解有關的數據。
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
36
|
package com.journaldev.annotations; import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class AnnotationParsing { public static void main(String[] args) { try { for (Method method : AnnotationParsing. class .getClassLoader() .loadClass(( 'com.journaldev.annotations.AnnotationExample' )) .getMethods()) { // checks if MethodInfo annotation is present for the method if (method.isAnnotationPresent(com.journaldev.annotations.MethodInfo. class )) { try { // iterates all the annotations available in the method for (Annotation anno : method.getDeclaredAnnotations()) { System.out.println( 'Annotation in Method ' '+ method + ' ' : ' + anno); } MethodInfo methodAnno = method.getAnnotation(MethodInfo. class ); if (methodAnno.revision() == 1 ) { System.out.println( 'Method with revision no 1 = ' + method); } } catch (Throwable ex) { ex.printStackTrace(); } } } } catch (SecurityException | ClassNotFoundException e) { e.printStackTrace(); } } } |
運行上面程序將輸出:
1
2
3
4
5
6
7
|
Annotation in Method 'public java.lang.String com.journaldev.annotations.AnnotationExample.toString()' : @com .journaldev.annotations.MethodInfo(author=Pankaj, revision= 1 , comments=Main method, date=Nov 17 2012 ) Method with revision no 1 = public java.lang.String com.journaldev.annotations.AnnotationExample.toString() Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @java .lang.Deprecated() Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @com .journaldev.annotations.MethodInfo(author=Pankaj, revision= 1 , comments=deprecated method, date=Nov 17 2012 ) Method with revision no 1 = public static void com.journaldev.annotations.AnnotationExample.oldMethod() Annotation in Method ' public static void com.journaldev.annotations.AnnotationExample.genericsTest |
以上就是java 自定義注解的實例詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/zmx729618/article/details/73163438