Spring AOP 自定義注解的實(shí)現(xiàn)代碼
1.在Maven中加入以下以依賴:
<!-- Spring AOP + AspectJ by shipengzhi --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.1_3</version> </dependency> <!-- end -->
在spring-***.xml中加入spring支持,打開aop功能
頭文件聲明 :
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd <!-- 自定義AOP --> <aop:aspectj-autoproxy proxy-target-class="true"> <aop:include name="controllerAspect" /> </aop:aspectj-autoproxy> <bean id="controllerAspect" class="com.sogou.upd.passport.common.aspect.ControllerAspect"></bean> //或: <aop:aspectj-autoproxy>
編寫自定義注解。實(shí)現(xiàn)對(duì)方法所實(shí)現(xiàn)的功能進(jìn)行描述,以便在通知中獲取描述信息
/* * 校驗(yàn)簽名合法性 自定義事務(wù) */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface SecureValid { String desc() default "身份和安全驗(yàn)證開始..."; }
@Target 用于描述注解的使用范圍(即:被描述的注解可以用在什么地方),其取值有:
取值 |
描述 |
CONSTRUCTOR |
用于描述構(gòu)造器。 |
FIELD |
用于描述域。 |
LOCAL_VARIABLE |
用于描述局部變量。 |
METHOD |
用于描述方法。 |
PACKAGE |
用于描述包。 |
PARAMETER |
用于描述參數(shù)。 |
TYPE |
用于描述類或接口(甚至 enum )。 |
@Retention 用于描述注解的生命周期(即:被描述的注解在什么范圍內(nèi)有效),其取值有:
取值 |
描述 |
SOURCE |
在源文件中有效(即源文件保留)。 |
CLASS |
在 class 文件中有效(即 class 保留)。 |
RUNTIME |
在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)。 |
@Documented 在默認(rèn)的情況下javadoc命令不會(huì)將我們的annotation生成再doc中去的,所以使用該標(biāo)記就是告訴jdk讓它也將annotation生成到doc中去
@Inherited 比如有一個(gè)類A,在他上面有一個(gè)標(biāo)記annotation,那么A的子類B是否不用再次標(biāo)記annotation就可以繼承得到呢,答案是肯定的
Annotation屬性值 有以下三種: 基本類型、數(shù)組類型、枚舉類型
1:基本串類型
public @interface UserdefinedAnnotation { intvalue(); String name(); String address(); }
使用:
@UserdefinedAnnotation(value=123,name="wangwenjun",address="火星") public static void main(String[] args) { System.out.println("hello"); } }
如果一個(gè)annotation中只有一個(gè)屬性名字叫value,我沒(méi)在使用的時(shí)候可以給出屬性名也可以省略。
public @interface UserdefinedAnnotation { int value(); }
也可以寫成如下的形式
@UserdefinedAnnotation(123) 等同于@UserdefinedAnnotation(value=123) public static void main(String[] args) { System.out.println("hello"); }
2:數(shù)組類型 我們?cè)谧远xannotation中定義一個(gè)數(shù)組類型的屬性,代碼如下:
public @interface UserdefinedAnnotation { int[] value(); }
使用:
public class UseAnnotation { @UserdefinedAnnotation({123}) public static void main(String[] args) { System.out.println("hello"); } }
注意1:其中123外面的大括號(hào)是可以被省略的,因?yàn)橹挥幸粋€(gè)元素,如果里面有一個(gè)以上的元素的話,花括號(hào)是不能被省略的哦。比如{123,234}。
注意2:其中屬性名value我們?cè)谑褂玫臅r(shí)候進(jìn)行了省略,那是因?yàn)樗衯alue,如果是其他名字我們就不可以進(jìn)行省略了必須是@UserdefinedAnnotation(屬性名={123,234})這樣的格式。
3:枚舉類型
public enum DateEnum { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday }
然后在定義一個(gè)annotation
package com.wangwenjun.annatation.userdefined; public @interface UserdefinedAnnotation { DateEnum week(); }
使用:
public class UseAnnotation { @UserdefinedAnnotation(week=DateEnum.Sunday) public static void main(String[] args) { System.out.println("hello"); } }
4:默認(rèn)值
public @interface UserdefinedAnnotation { String name() default "zhangsan"; }
使用:
public class UseAnnotation { @UserdefinedAnnotation() public static void main(String[] args) { System.out.println("hello"); } }
5:注意
Annotation是不可以繼承其他接口的,這一點(diǎn)是需要進(jìn)行注意,這也是annotation的一個(gè)規(guī)定吧。
Annotation也是存在包結(jié)構(gòu)的,在使用的時(shí)候直接進(jìn)行導(dǎo)入即可。
Annotation類型的類型只支持原聲數(shù)據(jù)類型,枚舉類型和Class類型的一維數(shù)組,其他的類型或者用戶自定義的類都是不可以作為annotation的類型,我查看過(guò)文檔并且進(jìn)行過(guò)測(cè)試。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)戰(zhàn)之電影在線觀看系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)電影在線觀看系統(tǒng),文中用到的技術(shù)有:JSP、Spring、SpringMVC、MyBatis等,感興趣的可以了解一下2022-04-04elasticsearch如何根據(jù)條件刪除數(shù)據(jù)
Elasticsearch是一個(gè)基于Apache Lucene?的開源搜索引擎,無(wú)論在開源還是專有領(lǐng)域,Lucene 可以被認(rèn)為是迄今為止最先進(jìn)、性能最好的、功能最全的搜索引擎庫(kù),這篇文章主要介紹了elasticsearch如何根據(jù)條件刪除數(shù)據(jù),需要的朋友可以參考下2023-03-03Java反射與Fastjson的危險(xiǎn)反序列化詳解
在?Java?中,Computer.class是一個(gè)引用,它表示了?Computer?的字節(jié)碼對(duì)象(Class對(duì)象),這個(gè)對(duì)象被廣泛應(yīng)用于反射、序列化等操作中,那么為什么?parseObject?需要這個(gè)引用呢,帶著這個(gè)問(wèn)題我們一起通過(guò)本文學(xué)習(xí)下吧2024-07-07解決java字符串轉(zhuǎn)換成時(shí)間Unparseable date出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決java字符串轉(zhuǎn)換成時(shí)間Unparseable date出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問(wèn)題
這篇文章主要介紹了IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Mybatis報(bào)錯(cuò)日志BindingException的解決
本文主要介紹了Mybatis報(bào)錯(cuò)日志BindingException的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07