Java注解Annotation详解
生活随笔
收集整理的這篇文章主要介紹了
Java注解Annotation详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
注解相當(dāng)于一種標(biāo)記,在程序中加了注解就等于為程序打上了某種標(biāo)記,沒加,則等于沒有某種標(biāo)記,以后,javac編譯器,開發(fā)工具和其他程序可以用反射來了解你的類及各種元素上有無何種標(biāo)記,看你有什么標(biāo)記,就去干相應(yīng)的事。標(biāo)記可以加在包,類,字段,方法,方法的參數(shù)以及局部變量上。 自定義注解及其應(yīng)用 1)、定義一個最簡單的注解 public @interface MyAnnotation { //...... } 2)、把注解加在某個類上: @MyAnnotation public class AnnotationTest{ //...... } 以下為模擬案例 自定義注解@MyAnnotation 1 package com.ljq.test;
2
3 ?import java.lang.annotation.ElementType;
4 ?import java.lang.annotation.Retention;
5 ?import java.lang.annotation.RetentionPolicy;
6 ?import java.lang.annotation.Target;
7
8 ?/**
9 * 定義一個注解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15 ?//Java中提供了四種元注解,專門負(fù)責(zé)注解其他的注解,分別如下
16
17 ?//@Retention元注解,表示需要在什么級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數(shù)包括:
18 ?//RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉
19 ?//RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認(rèn))
20 ?//RetentionPolicy.RUNTIME:內(nèi)存中的字節(jié)碼,VM將在運行時也保留注解,因此可以通過反射機制讀取注解的信息
21
22 ?//@Target元注解,默認(rèn)值為任何元素,表示該注解用于什么地方。可用的ElementType參數(shù)包括
23 ?//ElementType.CONSTRUCTOR: 構(gòu)造器聲明
24 ?//ElementType.FIELD: 成員變量、對象、屬性(包括enum實例)
25 ?//ElementType.LOCAL_VARIABLE: 局部變量聲明
26 ?//ElementType.METHOD: 方法聲明
27 ?//ElementType.PACKAGE: 包聲明
28 ?//ElementType.PARAMETER: 參數(shù)聲明
29 ?//ElementType.TYPE: 類、接口(包括注解類型)或enum聲明
30
31 ?//@Documented將注解包含在JavaDoc中
32
33 ?//@Inheried允許子類繼承父類中的注解
34 ?
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38 ?public @interface MyAnnotation {
39 //為注解添加屬性
40 ? String color();
41 String value() default "我是林計欽"; //為屬性提供默認(rèn)值
42 ? int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44 ? MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期為1988-2-18");
45 //添加枚舉屬性
46 ?
47 } 注解測試類AnnotationTest 1 package com.ljq.test;
2
3 ?/**
4 * 注解測試類
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10 ?//調(diào)用注解并賦值
11 ?@MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期為1988-2-18"),color="red", array={23, 26})
12 ?public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation注解
16 ? if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就獲取注解
18 ? MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //獲取注解屬性
21 ? System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數(shù)組
24 ? int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29 ? Gender gender=annotation.gender();
30 System.out.println("性別為:"+gender);
31 //獲取注解屬性
32 ? MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 } 枚舉類Gender,模擬注解中添加枚舉屬性 1 package com.ljq.test;
2 ?/**
3 * 枚舉,模擬注解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8 ?public enum Gender {
9 MAN{
10 public String getName(){return "男";}
11 },
12 WOMEN{
13 public String getName(){return "女";}
14 }; //記得有“;”
15 ? public abstract String getName();
16 } 注解類MetaAnnotation,模擬注解中添加注解屬性 1 package com.ljq.test;
2
3 ?/**
4 * 定義一個注解,模擬注解中添加注解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9 ?public @interface MetaAnnotation {
10 String birthday();
11 }
2
3 ?import java.lang.annotation.ElementType;
4 ?import java.lang.annotation.Retention;
5 ?import java.lang.annotation.RetentionPolicy;
6 ?import java.lang.annotation.Target;
7
8 ?/**
9 * 定義一個注解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15 ?//Java中提供了四種元注解,專門負(fù)責(zé)注解其他的注解,分別如下
16
17 ?//@Retention元注解,表示需要在什么級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數(shù)包括:
18 ?//RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉
19 ?//RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認(rèn))
20 ?//RetentionPolicy.RUNTIME:內(nèi)存中的字節(jié)碼,VM將在運行時也保留注解,因此可以通過反射機制讀取注解的信息
21
22 ?//@Target元注解,默認(rèn)值為任何元素,表示該注解用于什么地方。可用的ElementType參數(shù)包括
23 ?//ElementType.CONSTRUCTOR: 構(gòu)造器聲明
24 ?//ElementType.FIELD: 成員變量、對象、屬性(包括enum實例)
25 ?//ElementType.LOCAL_VARIABLE: 局部變量聲明
26 ?//ElementType.METHOD: 方法聲明
27 ?//ElementType.PACKAGE: 包聲明
28 ?//ElementType.PARAMETER: 參數(shù)聲明
29 ?//ElementType.TYPE: 類、接口(包括注解類型)或enum聲明
30
31 ?//@Documented將注解包含在JavaDoc中
32
33 ?//@Inheried允許子類繼承父類中的注解
34 ?
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38 ?public @interface MyAnnotation {
39 //為注解添加屬性
40 ? String color();
41 String value() default "我是林計欽"; //為屬性提供默認(rèn)值
42 ? int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44 ? MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期為1988-2-18");
45 //添加枚舉屬性
46 ?
47 } 注解測試類AnnotationTest 1 package com.ljq.test;
2
3 ?/**
4 * 注解測試類
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10 ?//調(diào)用注解并賦值
11 ?@MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期為1988-2-18"),color="red", array={23, 26})
12 ?public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation注解
16 ? if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就獲取注解
18 ? MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //獲取注解屬性
21 ? System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數(shù)組
24 ? int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29 ? Gender gender=annotation.gender();
30 System.out.println("性別為:"+gender);
31 //獲取注解屬性
32 ? MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 } 枚舉類Gender,模擬注解中添加枚舉屬性 1 package com.ljq.test;
2 ?/**
3 * 枚舉,模擬注解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8 ?public enum Gender {
9 MAN{
10 public String getName(){return "男";}
11 },
12 WOMEN{
13 public String getName(){return "女";}
14 }; //記得有“;”
15 ? public abstract String getName();
16 } 注解類MetaAnnotation,模擬注解中添加注解屬性 1 package com.ljq.test;
2
3 ?/**
4 * 定義一個注解,模擬注解中添加注解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9 ?public @interface MetaAnnotation {
10 String birthday();
11 }
總結(jié)
以上是生活随笔為你收集整理的Java注解Annotation详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刚才我提出要把数据库处理部分放到代码里,
- 下一篇: apache2 指令存取