java注解編程@since 1.8


一、基本元注解:    

  @Retention:

    說明這個注解的生命周期 

    RetentionPolicy.SOURCE -> 保留在原碼階段,編譯時忽略

    RetentionPolicy.CLASS -> 保留在編譯階段,不會被加載到jvm

    RetentionPolicy.RUNTIME -> 加載到jvm運行中  

  @Target:

    指明注解運用之處

    ElementType.Type -> 作用於類、接口、枚舉

    ElementType.FIELD -> 屬性

    ElementType.METHOD -> 方法

    ElementType.PARAMETER -> 方法的參數 

    ElementType.CONSTRUCTOR -> 構造方法

    ElementType.LOCAL_VARIABLE -> 局部變量

    ElementType.ANNOTATION_TYPE -> 作用於注解

    ElementType.PACKAGE -> 作用於包

    ElementType.TYPE_PARAMETER

    ElementType.TYPE_USE

  @Documented:

    表示文檔  

  @Inherited:

    表示繼承,作用在父類時子類可以擁有父類的注解

  @Repeatable:

    表明標記的注解維護一個容器,並且可以多次應用於相同的屬性或聲明

    容器注解:

       

    具體注解:

         

      作用於方法:

         

 

 

二、注解的屬性:

  1、注解只有成員變量,沒有方法。注解的成員變量在注解的定義中以“無形參的方法”形式來聲明,其方法名定義了該成員變量的名字,其返回值定義了該成員變量的類型。

  2、屬性時它的類型必須是 8 種基本數據類型外加 類、接口、注解及它們的數組。數組屬性在使用時可以加{}表示數組,也可以不加表示數組中只有一個元素。

  

三、注解的綜合運用:

  注解一般結合反射使用,通過反射模板對象獲取注解的類型和屬性值。

  注解與反射:

    1、java1.5在反射包中引入了 java.lang.reflect.AnnotatedElement 接口,該接口主要用於注解類型和屬性的處理。

    Class、Field、Method類的父類或超類都有實現AnnotatedElement 接口。

      

 

 

     該類的子類Field、Method等關於AnnotatedElement 的方法必須被重寫

      

     2、AnnotatedElement 接口的基本方法:

      isAnnotationPresent(); 判斷該元素上是否存在某個類型的注解。 
      getAnnotation()/getDeclaredAnnotation(); 通過注解類型獲取注解對象,從而獲取注解的屬性值。
      getAnnotations()/getDeclaredAnnotations(); 獲取注解集合。

   3、代碼片段:
 public static void main(String[] arg) throws NoSuchMethodException, NoSuchFieldException, SecurityException{ Class<UserController> userClass = UserController.class; // 獲取類上的注解 // 判斷該對象是否有該注解
        if (userClass.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping = userClass.getDeclaredAnnotation(RequestMapping.class); String[] value = requestMapping.value(); System.out.println("UserController類上RequestMapping注解的屬性:" + Arrays.toString(value)); }else { System.out.println("UserController類上沒有RequestMapping注解!"); } // 獲取屬性上的注解
        Field getField = userClass.getDeclaredField("userService"); SuperBug fSuperBug = getField.getDeclaredAnnotation(SuperBug.class); String[] fValue = fSuperBug.value(); System.out.println("userService屬性上SuperBug注解的值:" + Arrays.toString(fValue)); // 獲取方法上的注解
        Method getMethod = userClass.getDeclaredMethod("getUserList1", String.class); SuperBug mSuperBug = getMethod.getDeclaredAnnotation(SuperBug.class); String[] mValue = mSuperBug.value(); System.out.println("getUserList1方法上SuperBug注解的值:" + Arrays.toString(mValue)); // 獲取方法上的所有注解反射模板class // getDeclaredAnnotations返回該元素上直接聲明的注釋(不包括繼承)
        Annotation[] annotations = getMethod.getDeclaredAnnotations(); for (Annotation annotation : annotations) { Class<? extends Annotation> aClass = annotation.annotationType(); if ("com.superbug.word.annotation.SuperBug".equals(aClass.getTypeName())) { SuperBug superBug1 = (SuperBug) annotation; System.out.println(superBug1.toString()); } } }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM