Java註解拾遺

來源:互聯網
上載者:User

標籤:express   res   local   his   this   屬性   orm   doc   pre   

註解簡介:     

        註解Annotation是jdk1.5的新增功能,在現在的日常開發中,幾乎離不開註解,寫篇短文,來做個拾遺。

註解作用:

        Annotation(註解)的作用是修飾包、類、構造方法、方法、成員變數等。

      註解文法及定義形式:

  @interface關鍵字定義
  註解包含成員,成員以無參數的方法的形式被聲明。其方法名和傳回值定義了該成員的名字和類型。
  成員賦值是通過@Annotation(name=value)的形式
  註解需要標明註解的生命週期,註解的修飾目標等資訊,這些資訊是通過元註解實現。

  舉例:

       

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target {    /**     * Returns an array of the kinds of elements an annotation type     * can be applied to.     * @return an array of the kinds of elements an annotation type     * can be applied to     */    ElementType[] value();}

       元註解@Retention,成員value的值為RetentionPolicy.RUNTIME、
       元註解@Target,成員value是個數組,用{}形式賦值,值為ElementType.ANNOTATION_TYPE
       成員名稱為value,類型為ElementType[]

註解分類:內建標準註解:

    @Override:用於修飾此方法覆蓋了父類的方法
     @Deprecated:用於修飾已經過時的方法
     @SuppressWarnnings:用於通知java編譯器禁止特定的編譯警告

元註解:

    @Target:說明Annotation所修飾的物件範圍
    @Retention:說明Annotation的生命週期
    @Documented :Documented是一個標記註解,主要是用於javadoc
    @Inherited :Inherited也是一個標記註解,假如一個標記了Inherited的註解修飾一個類,那麼繼承於此類的子類,同樣繼承該註解
          這樣雖然完成了繼承,但是註解本身是不允許繼承的。

普通註解:

    自訂的完成特定任務的註解

註解生命週期:

    註解的生命週期是由元註解Retention修飾的
    看下Retention的源碼
    

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value();}public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time. This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME}

 

SOURCE: 註解只保留在源檔案,當Java檔案編譯成class檔案的時候,註解被遺棄
CLASS: 註解被保留到class檔案,jvm載入class檔案時候被遺棄。這是預設的生命週期
RUNTIME: 註解不僅被儲存到class檔案中,jvm載入class檔案之後,仍然存在,儲存到class對象中,可以通過反射來擷取

註解的修飾目標:

  註解的修飾目標是由元註解Target修飾的

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target {/*** Returns an array of the kinds of elements an annotation type* can be applied to.* @return an array of the kinds of elements an annotation type* can be applied to*/ElementType[] value();}public enum ElementType {/** Class, interface (including annotation type), or enum declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE}

 

TYPE:指的是在類,介面(包括註解)或者enum上使用的註解
FIELD:指的在field屬性,也包括enum常量使用的註解
METHOD:指的是在方法聲明上使用的註解
PARAMETER:指的是在參數上使用的註解
CONSTRUCTOR: 指的是在構造器使用的註解
LOCAL_VARIABLE:指的是在局部變數上使用的註解
ANNOTATION_TYPE:指的是在註解上使用的元註解
PACKAGE:指的是在包上使用的註解

自訂註解:

  自訂註解是利用註解方面很重要的一個方面,這次把項目裡的一個自訂的註解分析下
代碼如下:

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface LogMethod {}@Slf4jpublic class LogMethodInterceptor implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {String className = String.valueOf(methodInvocation.getMethod().getDeclaringClass());String methodName = methodInvocation.getMethod().getName();Object[] arguments = methodInvocation.getArguments();log.info("Before:" + className + "." + methodName + "()");log.info("Arguments:" + JsonTools.toJsonString(arguments));long start = System.currentTimeMillis();Object resultObj = methodInvocation.proceed();long end = System.currentTimeMillis();log.info("After:result is " + JsonTools.toJsonString(resultObj) + ",the execute time is " + (end - start) + " ms");return resultObj;}}

 

具體配置:

<bean id="logMethodInterceptor" class="com.*LogMethodInterceptor"/><aop:config proxy-target-class="true"><aop:pointcut id="logMethodPointcut"expression="execution(* com*service.impl.*.*(..)) and @annotation(com.*annotation.LogMethod)"/><aop:advisor advice-ref="logMethodInterceptor" pointcut-ref="logMethodPointcut"/></aop:config>

LogMethod 定義了一個 生命週期為RUNTIME 作用在METHOD上的註解
LogMethodInterceptor 註解的處理器
利用Spring的AOP完整在方法的出入口分別列印參數的相關日誌

 

Java註解拾遺

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.