基於自定義註解annotation 例項

2021-07-25 19:55:47 字數 935 閱讀 8199

1,自定義註解格式:

@target(elementtype.method)

@retention(retentionpolicy.runtime)

public @inte***ce needjsonnullreplace

2,註解實現類

public class jsonnullreplaceadvice else if(returnvalue instanceof jsonarray)else if(returnvalue instanceof resultinfovo)else if(obj instanceof jsonarray)

}}}

3,面向切面配置

4,特殊說明

@target說明了annotation所修飾的物件範圍:

1.constructor:用於描述構造器

2.field:用於描述域

3.local_variable:用於描述區域性變數

4.method:用於描述方法

5.package:用於描述包

6.parameter:用於描述引數

7.type:用於描述類、介面(包括註解型別) 或enum宣告

2,@retention

定義了該annotation被保留的時間長短

1.source:在原始檔中有效(即原始檔保留)

2.class:在class檔案中有效(即class保留)

3.runtime:在執行時有效(即執行時保留)

3,配置aop 相關參考

5,使用:

只需要在要實現過濾的方法上加入@needjsonnullreplace  即可。

自定義註解

target elementtype.field retention retentionpolicy.runtime public inte ce setvalue以上就是乙個自定義的註解,下面來進行說明。target elementtype.field 表示支援該註解的程式元素,field就是屬性...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...