循序漸進之Spring AOP(6)
前面幾節的示例看起來讓人沮喪,要記憶如此多的介面、類和繼承關係,做各種複雜的配置。好在這些只是一種相對過時的實現方式,現在只需要使用@Aspect註解及表示式就可以輕鬆的使用POJO來定義切面,設計精妙正如Spring MVC的@Controller。
1 示例
仍然使用上一節的"騎士和劍士"的例子,目標類Horseman和Swordman
public class Horseman { public void rush(String enemy) { System.out.println(this.getClass().getSimpleName() + "衝刺攻擊" + enemy); } public void chop(String enemy) { System.out.println(this.getClass().getSimpleName() + "砍劈攻擊" + enemy); } }
public class Swordman {
public void block(String enemy) {
System.out.println(this.getClass().getSimpleName() + "格擋" + enemy);
}
public void chop(String enemy) {
System.out.println(this.getClass().getSimpleName() + "砍劈攻擊" + enemy);
}
}
切面
applicationContext.xml@Aspect public class StorageAdvisor { @Before("execution(* chop(..))") public void beforeAttack(JoinPoint point) { System.out.println("Advice: " + point.getTarget().getClass().getSimpleName() + "蓄力"); } }
測試程式碼<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy /> <bean id="horseman" class="examples.chap03.Horseman" /> <bean id="swordman" class="examples.chap03.Swordman" /> <bean class="examples.chap03.StorageAdvisor" /> </beans>
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("examples/chap03/applicationContext.xml");
Horseman hm = (Horseman)context.getBean("horseman");
hm.rush("Ghoul");
hm.chop("Ghoul");
Swordman sm = (Swordman)context.getBean("swordman");
sm.block("Ghoul");
sm.chop("Ghoul");
}
對比上一節的內容可以看到,實現同樣的功能,程式碼和配置都精簡了很多,而且構建切面的類是一個普通的POJO,開發和測試的靈活性都大幅度提高。
2 註解說明
2.1 @Aspect
作用是把當前類標識為一個切面供容器讀取
2.2 @Before
標識一個前置增強方法,相當於BeforeAdvice的功能,相似功能的還有
2.3 @AfterReturning
後置增強,相當於AfterReturningAdvice,方法正常退出時執行
2.4 @AfterThrowing
異常丟擲增強,相當於ThrowsAdvice
2.5 @After
final增強,不管是丟擲異常或者正常退出都會執行
2.6 @Around
環繞增強,相當於MethodInterceptor
2.7 @DeclareParents
引介增強,相當於IntroductionInterceptor
3 execution切點函式
execution函式用於匹配方法執行的連線點,語法為:
execution(方法修飾符(可選) 返回型別 方法名 引數 異常模式(可選))
引數部分允許使用萬用字元:
* 匹配任意字元,但只能匹配一個元素
.. 匹配任意字元,可以匹配任意多個元素,表示類時,必須和*聯合使用
+ 必須跟在類名後面,如Horseman+,表示類本身和繼承或擴充套件指定類的所有類
示例中的* chop(..)解讀為:
方法修飾符 無
返回型別 *匹配任意數量字元,表示返回型別不限
方法名 chop表示匹配名稱為chop的方法
引數 (..)表示匹配任意數量和型別的輸入引數
異常模式 不限
更多示例:
void chop(String,int)
匹配目標類任意修飾符方法、返回void、方法名chop、帶有一個String和一個int型引數的方法
public void chop(*)
匹配目標類public修飾、返回void、方法名chop、帶有一個任意型別引數的方法
public String *o*(..)
匹配目標類public修飾、返回String型別、方法名中帶有一個o字元、帶有任意數量任意型別引數的方法
public void *o*(String,..)
匹配目標類public修飾、返回void、方法名中帶有一個o字元、帶有任意數量任意型別引數,但第一個引數必須有且為String型的方法
也可以指定類:
public void examples.chap03.Horseman.*(..)
匹配Horseman的public修飾、返回void、不限方法名、帶有任意數量任意型別引數的方法
public void examples.chap03.*man.*(..)
匹配以man結尾的類中public修飾、返回void、不限方法名、帶有任意數量任意型別引數的方法
指定包:
public void examples.chap03.*.chop(..)
匹配examples.chap03包下所有類中public修飾、返回void、方法名chop、帶有任意數量任意型別引數的方法
public void examples..*.chop(..)
匹配examples.包下和所有子包中的類中public修飾、返回void、方法名chop、帶有任意數量任意型別引數的方法
可以用這些表示式替換StorageAdvisor中的程式碼並觀察效果
4 更多切點函式
除了execution(),Spring中還支援其他多個函式,這裡列出名稱和簡單介紹,以方便根據需要進行更詳細的查詢
4.1 @annotation()
表示標註了指定註解的目標類方法
例如 @annotation(org.springframework.transaction.annotation.Transactional) 表示標註了@Transactional的方法
4.2 args()
通過目標類方法的引數型別指定切點
例如 args(String) 表示有且僅有一個String型引數的方法
4.3 @args()
通過目標類引數的物件型別是否標註了指定註解指定切點
如 @args(org.springframework.stereotype.Service) 表示有且僅有一個標註了@Service的類引數的方法
4.4 within()
通過類名指定切點
如 with(examples.chap03.Horseman) 表示Horseman的所有方法
4.5 target()
通過類名指定,同時包含所有子類
如 target(examples.chap03.Horseman) 且Elephantman extends Horseman,則兩個類的所有方法都匹配
4.6 @within()
匹配標註了指定註解的類及其所有子類
如 @within(org.springframework.stereotype.Service) 給Horseman加上@Service標註,則Horseman和Elephantman 的所有方法都匹配
4.7 @target()
所有標註了指定註解的類
如 @target(org.springframework.stereotype.Service) 表示所有標註了@Service的類的所有方法
4.8 this()
大部分時候和target()相同,區別是this是在執行時生成代理類後,才判斷代理類與指定的物件型別是否匹配
5 邏輯運算子
表示式可由多個切點函式通過邏輯運算組成
5.1 &&
與操作,求交集,也可以寫成and
例如 execution(* chop(..)) && target(Horseman) 表示Horseman及其子類的chop方法
5.2 ||
或操作,求並集,也可以寫成or
例如 execution(* chop(..)) || args(String) 表示名稱為chop的方法或者有一個String型引數的方法
5.3 !
非操作,求反集,也可以寫成not
例如 execution(* chop(..)) and !args(String) 表示名稱為chop的方法但是不能是隻有一個String型引數的方法