<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 0,  trackbacks - 0
    annotation到底能起什么作用呢?
    1,    編譯工具或其他工具可以根據被附加在代碼里的annotation信息自動生成配置文件或文檔等外部文件。
    比如,sun公司就提供了apt(Annotation Processing Tool)工具,apt工具是一個可以處理annotation的命令行工具,apt提供了在編譯期針對源代碼級別的解析,并可以在解析時生成新的源代碼和其他文件,同時還可以對生成的源代碼進行編譯。
    2,    其他程序可以在運行時動態解析將要被執行的程序里的annotation信息,并根據被附加的annotation信息來執行不同的操作。
    比如,EJB3規范就比較廣泛地使用了annotation特性。比如只要在POJO為class注明@Stateless注釋,EJB容器便會根據此annotation把該POJO注冊為無狀態的Session Bean。EJB3使用了annotation大大地簡化了EJB的開發和配置過程。我們會在其他文章里專門介紹EJB Annotation的原理與使用方法,這里不做詳述。

    本文通過一個簡單地例子來說明怎么在運行期動態解析annotation。Apt工具的使用我們會在近期其他文章里對其加以介紹。

    比如,我們定義了MyAnnotation3注釋:
     MyAnnotation3.java
    package com.test.annotation;

    import java.lang.annotation.Annotation;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation3 {
        public String value();
        public String[] multiValues();
        int number() default 0;
    }
    上面定義了一個名為MyAnnotation3的注釋。

    我們再定義一個GetMyAnnotation類,該類使用了MyAnnotation3注釋:
    GetMyAnnotation.java:
    package com.test.annotation.test;

    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;

    import javax.ejb.EJB;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    import com.test.annotation.MyAnnotation3;

    // 為GetMyAnnotation類附加MyAnnotation3 注釋
    @MyAnnotation3(value = "Class GetMyAnnotation", multiValues = {"1","2"})
    public class GetMyAnnotation {
        // 為testField1屬性附加MyAnnotation3 注釋
        @MyAnnotation3(value = "call testField1", multiValues={"1"}, number = 1)
        private String testField1;

        // 為testMethod1方法附加MyAnnotation3 注釋
        @MyAnnotation3(value = "call testMethod1", multiValues={"1", "2"}, number = 1)
        public void testMethod1() {

        }

        @Deprecated
        @MyAnnotation3(value = "call testMethod2", multiValues={"3", "4", "5"})
        public void testMethod2() {

        }
    }
    上面的例子GetMyAnnotation非常簡單,里面沒有任何功能,但是分別為類(class),屬性(field),方法(method)申明(附加)了MyAnnotation3 注釋。

    下面我們用程序TestMyAnnotation3對GetMyAnnotation里MyAnnotation3注釋進行解析。

    運行時解析annotation
    TestMyAnnotation3.java
    public class TestMyAnnotation3 {

        public static void main(String[] args) {
            System.out.println("--Class Annotations--");
            if (GetMyAnnotation.class.isAnnotationPresent(MyAnnotation3.class)) {
                System.out.println("[GetMyAnnotation].annotation:");
                MyAnnotation3 classAnnotation = GetMyAnnotation.class.getAnnotation(MyAnnotation3.class);
                printMyAnnotation3(classAnnotation);
            }
            
            
            System.out.println("--Fields Annotations--");
            Field [] fields = GetMyAnnotation.class.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(MyAnnotation3.class)) {
                    System.out.println("[GetMyAnnotation." + field.getName() + "].annotation:");
                    MyAnnotation3 fieldAnnotation = field.getAnnotation(MyAnnotation3.class);
                    printMyAnnotation3(fieldAnnotation);
                }
            }
            
            System.out.println("--Methods Annotations--");
            Method[] methods = GetMyAnnotation.class.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println("[GetMyAnnotation." + method.getName() + "].annotation:");
                if (method.isAnnotationPresent(MyAnnotation3.class)) {
                    MyAnnotation3 methodAnnotation = method.getAnnotation(MyAnnotation3.class);
                    printMyAnnotation3(methodAnnotation);  
                }
            }
        }
        
        private static void printMyAnnotation3(MyAnnotation3 annotation3) {
            if (annotation3 == null) {
                return;
            }
            
            System.out.println("{value=" + annotation3.value());
            
            String multiValues = "";
            for (String value: annotation3.multiValues()) {
                multiValues += "," + value;
            }
            
            System.out.println("multiValues=" + multiValues);
            
            System.out.println("number=" + annotation3.number() + "}");
        }
    }

    輸出:
    --Class Annotations--
    [GetMyAnnotation].annotation:
    {value=Class GetMyAnnotation
    multiValues=,1,2
    number=0}
    --Fields Annotations--
    [GetMyAnnotation.testField1].annotation:
    {value=call testField1
    multiValues=,1
    number=1}
    --Methods Annotations--
    [GetMyAnnotation.testMethod1].annotation:
    {value=call testMethod1
    multiValues=,1,2
    number=1}
    [GetMyAnnotation.testMethod2].annotation:
    {value=call testMethod2
    multiValues=,3,4,5
    number=0}


    JDK1.5以后的版本提供的跟annotation有關的接口:
    interface java.lang.reflect.AnnotatedElement {
        boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
        <T extends Annotation> T getAnnotation(Class<T> annotationClass);
        Annotation[] getAnnotations();
        Annotation[] getDeclaredAnnotations();
    }
    該接口主要用來取得附加在類(class),構造方法(constructor),屬性(field),方法(method),包(package)上的annotation信息。

    JDK1.5與此有關的幾個類都實現了AnnotatedElement接口:
    java.lang.reflect.AccessibleObject,
    java.lang.reflect.Class,
    java.lang.reflect.Constructor,
    java.lang.reflect.Field,
    java.lang.reflect.Method,
    java.lang.reflect.Package
    所以可以利用反射(reflection)功能在程序里動態解析附加的annotation信息。


    總結:
    本文通過舉例簡單地說明了怎么動態解析annotation,大家可以舉一反三,利用Java的annotation特性,完成更復雜功能等。    
    posted on 2009-02-27 17:23 ID刀 閱讀(2509) 評論(0)  編輯  收藏 所屬分類: JAVA_Base

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(2)

    隨筆檔案(1)

    文章分類(21)

    文章檔案(17)

    最新隨筆

    搜索

    •  

    積分與排名

    • 積分 - 13665
    • 排名 - 2090

    最新評論

    主站蜘蛛池模板: 免费看国产精品麻豆| 97人妻无码一区二区精品免费| 在线观看人成视频免费| 蜜桃视频在线观看免费视频网站WWW | 毛片免费观看视频| 亚洲va成无码人在线观看| 精品久久久久久久久免费影院| 亚洲日韩精品无码专区网站| www免费插插视频| 亚洲精品自在在线观看| 精品视频一区二区三区免费| 久久青青草原亚洲av无码app| 免费精品视频在线| 永久中文字幕免费视频网站| 亚洲精品一卡2卡3卡三卡四卡| 蜜桃视频在线观看免费视频网站WWW| 亚洲精品无码久久久久久久| 成人毛片免费视频| 美女羞羞免费视频网站| 亚洲五月综合缴情在线观看| 久9热免费精品视频在线观看| 亚洲第一成年网站大全亚洲| 成人片黄网站A毛片免费| 色偷偷亚洲第一综合网| 亚洲日本va在线视频观看| 无码国产精品一区二区免费模式| 亚洲国产成人手机在线电影bd | 亚洲国产精品张柏芝在线观看| www.黄色免费网站| 日韩在线一区二区三区免费视频| 亚洲人成中文字幕在线观看| 黄色成人免费网站| 精品韩国亚洲av无码不卡区 | 亚洲成a人片在线观看无码| 无码国产精品一区二区免费 | 人妖系列免费网站观看| 久久夜色精品国产噜噜噜亚洲AV| 处破痛哭A√18成年片免费| 和老外3p爽粗大免费视频| 亚洲啪啪免费视频| 中文字幕亚洲一区二区va在线|