1.在Maven中加入以下以依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<!-- 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功能
頭文件聲明 :
1
2
3
4
5
6
7
8
9
10
11
|
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)行描述,以便在通知中獲取描述信息
1
2
3
4
5
6
7
8
9
10
|
/* * 校驗(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:基本串類型
1
2
3
4
5
|
public @interface UserdefinedAnnotation { intvalue(); String name(); String address(); } |
使用:
1
2
3
4
5
|
@UserdefinedAnnotation (value= 123 ,name= "wangwenjun" ,address= "火星" ) public static void main(String[] args) { System.out.println( "hello" ); } } |
如果一個(gè)annotation中只有一個(gè)屬性名字叫value,我沒在使用的時(shí)候可以給出屬性名也可以省略。
1
2
3
|
public @interface UserdefinedAnnotation { int value(); } |
也可以寫成如下的形式
1
2
3
4
|
@UserdefinedAnnotation ( 123 ) 等同于 @UserdefinedAnnotation (value= 123 ) public static void main(String[] args) { System.out.println( "hello" ); } |
2:數(shù)組類型 我們?cè)谧远xannotation中定義一個(gè)數(shù)組類型的屬性,代碼如下:
1
2
3
|
public @interface UserdefinedAnnotation { int [] value(); } |
使用:
1
2
3
4
5
6
7
|
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:枚舉類型
1
2
3
|
public enum DateEnum { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday } |
然后在定義一個(gè)annotation
1
2
3
4
5
|
package com.wangwenjun.annatation.userdefined; public @interface UserdefinedAnnotation { DateEnum week(); } |
使用:
1
2
3
4
5
6
|
public class UseAnnotation { @UserdefinedAnnotation (week=DateEnum.Sunday) public static void main(String[] args) { System.out.println( "hello" ); } } |
4:默認(rèn)值
1
2
3
|
public @interface UserdefinedAnnotation { String name() default "zhangsan" ; } |
使用:
1
2
3
4
5
6
|
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的類型,我查看過文檔并且進(jìn)行過測(cè)試。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/changliangwl/article/details/51498345