此例子,用于說(shuō)明如何在Java中對(duì)“注解 Annotation”的定義、使用和解析的操作。注解一般用于自定義開(kāi)發(fā)框架中,至于為什么使用,此處不作過(guò)多說(shuō)明,這里只說(shuō)明如何使用,以作備記。下面例子已測(cè)試,可以正常運(yùn)行通過(guò)。
1、注解自定義。
這里定義兩個(gè)注解,分別用來(lái)注解類和注解屬性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package cc.rulian.ann; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 類注釋 */ @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.TYPE) public @interface MyTable { public String name() default "" ; public String version() default "1" ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package cc.rulian.ann; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 字段注釋 */ @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.FIELD) public @interface MyField { public String name() default "" ; //名稱 public String type() default "" ; //類型 } |
2、注解的使用。
說(shuō)明如何在類中使用自定義注解。
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package cc.rulian.ann; import java.util.Date; /** * 基礎(chǔ)日志 * */ @MyTable (name= "T_BaseLog" ,version= "2" ) public class BaseLog{ @MyField (name= "addTime" ,type= "Date" ) private Date log_time; // 時(shí)間 @MyField (name= "log_level" ,type= "String" ) private String log_level; // 級(jí)別 @MyField (name= "message" ,type= "String" ) private String message; // 日志內(nèi)容 public Date getLog_time() { return log_time; } public void setLog_time(Date log_time) { this .log_time = log_time; } public String getLog_level() { return log_level; } public void setLog_level(String log_level) { this .log_level = log_level; } public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } } |
3、注解的解析。
說(shuō)明如何解析注解。
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
|
package cc.rulian.ann; import java.lang.reflect.Field; /** * 讀取注釋 */ public class ReadAnn { public static void main(String[] args) { // 讀取類的注釋 BaseLog obj = new BaseLog(); // Annotation[] arr = obj.getClass().getAnnotations(); //得到所有注釋 MyTable table = obj.getClass().getAnnotation(MyTable. class ); // 取得指定注釋 System.out.println( "類注釋(name): " + table.name()); System.out.println( "類注釋(version): " + table.version()); // 讀取屬性的注釋 Field[] fields = obj.getClass().getDeclaredFields(); for (Field f : fields) { // Annotation[] arr2 = f.getAnnotations(); //得到所有注釋 MyField ff = f.getAnnotation(MyField. class ); // 取得指定注釋 if (ff != null ) { System.out.println( "屬性(" + f.getName() + "): " + ff.name() + " -- " + ff.type()); } } } } |
4、解析輸出結(jié)果。
類注釋(name): T_BaseLog
類注釋(version): 2
屬性(log_time): addTime -- Date
屬性(log_level): log_level -- String
屬性(message): message -- String
以上這篇淺談Java中注解Annotation的定義、使用、解析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。