Java注解是java5版本發(fā)布的,其作用就是節(jié)省配置文件,增強(qiáng)代碼可讀性。在如今各種框架及開發(fā)中非常常見,特此說明一下。
如何創(chuàng)建一個(gè)注解
每一個(gè)自定義的注解都由四個(gè)元注解組成,這四個(gè)元注解由java本身提供:
@Target(ElementType.**)
這是一個(gè)枚舉,它置頂是該自定義的注解使用的地方,像類、變量、方法等
@Retention(RetentionPolicy.**)作用是標(biāo)明注解保存在什么級(jí)別,像在編譯時(shí)、class文件中,vm運(yùn)行中
@Documented 將此注解包含在 javadoc 中 ,它代表著此注解會(huì)被javadoc工具提取成文檔。在doc文檔中的內(nèi)容會(huì)因?yàn)榇俗⒔獾男畔?nèi)容不同而不同
@Inherited : 在您定義注解后并使用于程序代碼上時(shí),預(yù)設(shè)上父類別中的注解并不會(huì)被繼承至子類別中,您可以在定義注解時(shí)加上java.lang.annotation.Inherited 限定的Annotation,這讓您定義的Annotation型別被繼承下來。
介紹完理論,開始代碼(talk is cheap,show your code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.yasin.JavaLearn; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 這是一個(gè)類級(jí)別的注釋,這個(gè)注釋中有一個(gè)name字段,默認(rèn)值是 yasin * @author yasin * */ @Target (ElementType.TYPE) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface Learn { String name() default "yasin" ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.yasin.JavaLearn; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 這是一個(gè)變量級(jí)別的注解,注解中有一個(gè)字段name,默認(rèn)值是field * @author yasin * */ @Target (ElementType.FIELD) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface FiledLearn { String name() default "field" ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.yasin.JavaLearn; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 這是一個(gè)方法級(jí)別的注解 * @author yasin * */ @Target (ElementType.METHOD) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface MethodLearn { String name() default "method" ; } |
上面了我定義了三個(gè)注解,分別是常用的類、變量、方法三個(gè)級(jí)別的注解。
下面我定義一個(gè)類,使用這三個(gè)注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.yasin.JavaLearn; @Learn public class Yasin { @FiledLearn public int level; @FiledLearn (name= "xq" ) public String xq; public String a; @MethodLearn (name= "test" ) public void setMain(){ } public void setA(){ } } |
下面就是如何使用這個(gè)注解了,注解的提取,都是通過class反射得到相應(yīng)的變量和方法,在從變量和方法中獲得注解。
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
52
53
54
|
package com.yasin.JavaLearn; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; /** * Hello world! * */ public class App { public static void main(String[] args) { Learn learn = Yasin. class .getAnnotation(Learn. class ); System.out.println(learn.name()); Field[] fields = Yasin. class .getFields(); //獲取該類所有的字段 for (Field filed:fields){ if (filed.isAnnotationPresent(FiledLearn. class )){ //校驗(yàn)該字段是否添加這個(gè)注解 System.out.println(filed.getName()); FiledLearn filedLearn = filed.getAnnotation(FiledLearn. class ); System.out.println(filedLearn.name()); } } Method[] methods = Yasin. class .getMethods(); for (Method method:methods){ if (method.isAnnotationPresent(MethodLearn. class )){ //校驗(yàn)該方法是否有這個(gè)注解 System.out.println(method.getName()); MethodLearn methodLearn = method.getAnnotation(MethodLearn. class ); System.out.println(methodLearn.name()); } } } } |
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/u013592964/article/details/70770337