1.在Maven中加入以下以依賴(lài):
<!-- 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支持,打開(kāi)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>
編寫(xiě)自定義注解。實(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)證開(kāi)始..."; } @Target 用于描述注解的使用范圍(即:被描述的注解可以用在什么地方),其取值有:
| 取值 | 描述 |
| CONSTRUCTOR | 用于描述構(gòu)造器。 |
| FIELD | 用于描述域。 |
| LOCAL_VARIABLE | 用于描述局部變量。 |
| METHOD | 用于描述方法。 |
| PACKAGE | 用于描述包。 |
| PARAMETER | 用于描述參數(shù)。 |
| TYPE | 用于描述類(lèi)或接口(甚至 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è)類(lèi)A,在他上面有一個(gè)標(biāo)記annotation,那么A的子類(lèi)B是否不用再次標(biāo)記annotation就可以繼承得到呢,答案是肯定的
Annotation屬性值 有以下三種: 基本類(lèi)型、數(shù)組類(lèi)型、枚舉類(lèi)型
1:基本串類(lèi)型
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(); } 也可以寫(xiě)成如下的形式
@UserdefinedAnnotation(123) 等同于@UserdefinedAnnotation(value=123) public static void main(String[] args) { System.out.println("hello"); } 2:數(shù)組類(lèi)型 我們?cè)谧远xannotation中定義一個(gè)數(shù)組類(lèi)型的屬性,代碼如下:
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:枚舉類(lèi)型
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類(lèi)型的類(lèi)型只支持原聲數(shù)據(jù)類(lèi)型,枚舉類(lèi)型和Class類(lèi)型的一維數(shù)組,其他的類(lèi)型或者用戶(hù)自定義的類(lèi)都是不可以作為annotation的類(lèi)型,我查看過(guò)文檔并且進(jìn)行過(guò)測(cè)試。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選