aspectj 是通過注解來描述切點與增強的。
1 開發(fā)環(huán)境要求
因為要使用注解,所以請確保使用的 java5.0 及以上版本。
引入 aspectj 相關(guān)類庫:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjrt</artifactid> <version>${aspectj.version}</version> </dependency> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>${aspectj.version}</version> </dependency> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjtools</artifactid> <version>${aspectj.version}</version> </dependency> <dependency> <groupid>aopalliance</groupid> <artifactid>aopalliance</artifactid> <version>${aopalliance.version}</version> </dependency> |
2 編程方式
@aspect//標(biāo)識切面
1
2
3
4
5
6
7
8
9
|
public class prerentaspect { /** * 增強邏輯 */ @before ( "execution(* rent(..))" ) //定義切點與增強類型 public void beforerent() { system.out.println( "開始執(zhí)行租賃動作" ); } } |
這個切面只是一個普通的 pojo,只不過加了 @aspect 注解。
@before("execution(* rent(..))")
中的 @before
表示增強類型是前置增強,它的內(nèi)容是 @aspectj 切點表達(dá)式,這里表示的是在目標(biāo)類的 rent() 方法上織入增強, rent() 可以包含任意入?yún)⒑腿我獾姆祷刂怠?/p>
帶 @aspect
的類,通過注解與代碼,將切點、增強類型和增強的橫切邏輯整合到了一起,是不是很方便呀o(∩_∩)o哈哈~
單元測試:
1
2
3
4
5
6
7
8
9
10
11
12
|
aspectjproxyfactory factory = new aspectjproxyfactory(); //設(shè)置目標(biāo)類 factory.settarget( new user()); //添加切面類 factory.addaspect(prerentaspect. class ); user proxy = factory.getproxy(); string userid = "001" ; proxy.rent(userid); proxy.back(userid); |
輸出結(jié)果:
--開始執(zhí)行租賃動作--
user:租賃【充電寶】
user:歸還【充電寶】
3 配置方式
1
2
3
4
5
6
7
8
9
|
<!-- 目標(biāo)類--> <bean id= "user" class = "net.deniro.spring4.aspectj.user" /> <!-- 切面類--> <bean class = "net.deniro.spring4.aspectj.prerentaspect" /> <!-- 自動創(chuàng)建代理--> <bean class = "org.springframework.aop.aspectj.annotation.annotationawareaspectjautoproxycreator" /> |
單元測試:
1
2
3
4
5
|
applicationcontext context = new classpathxmlapplicationcontext(spring.xml"); user user = (user) context.getbean( "user" ); string userid = "001" ; user.rent(userid); user.back(userid); |
輸出結(jié)果與編程方式完全相同。
也可以基于 schema 的 aop 命名空間進(jìn)行配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!--aspectj 驅(qū)動器 --> <aop:aspectj-autoproxy/> <!-- 目標(biāo)類--> <bean id= "user" class = "net.deniro.spring4.aspectj.user" /> <!-- 切面類--> <bean class = "net.deniro.spring4.aspectj.prerentaspect" /> </beans> |
這樣的配置更加簡潔。其實在 <aop:aspectj-atuoproxy/>
內(nèi)部已經(jīng)采用了自動代理模式啦 o(∩_∩)o哈哈~
<aop:aspectj-atuoproxy/>
的 proxy-target-class
屬性,默認(rèn)為 false ,表示使用 jdk 動態(tài)代理技術(shù)織入增強;此值為 true 則表示使用 cglib 動態(tài)代理技術(shù)織入增強 。 如果目標(biāo)類沒有聲明接口,那么即使 proxy-target-class
設(shè)置為 false,也會自動使用 cglib 動態(tài)代理織入增強的喲o(∩_∩)o哈哈~
基于 java5.0+ 的項目,建議使用 aspectj 來配置切點與增強,因為這樣更簡潔、也更直接。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/abf28c0a15b0