Spring提供了4種類型的AOP支持:
- 基于代理的經典Spring AOP;
- 純POJO切面;
- @AspectJ注解驅動的切面;
- 注入式AspectJ切面(適用于Spring各版本)
因為Spring基于動態代理,所以Spring只支持方法連接點。這與一些其他的AOP框架是不同的,例如AspectJ和JBoss,除了方法切點,它們還提供了字段和構造器接入點。
一、定義切點
在Spring AOP中,要使用AspectJ的切點表達式語言來定義切點。以下是AspectJ切點指示器:
- arg(),限制連接點匹配參數為指定類型的執行方法
- @args(),限制連接點匹配參數由指定注解標注的執行方法
- execution(),用于匹配是連接點的執行方法
- target,限制連接點匹配目標對象為指定類型的類
- @target(),限制連接點匹配特定的執行對象,這些對象對應的類要具有指定類型的注解
- within(),限制連接點匹配指定的類型
- @within(),限制連接點匹配指定注解所標注的類型(當使用Spring AOP時,方法定義在由指定的注解所標注的類里)
- @annotation,限定匹配帶有指定注解的連接點
只有execution指示器是實際執行匹配的,而其他的指示器都是用來限制匹配的。
切點表達式,這個表達式能夠設置當perform()方 法執行時觸發通知的調用
方法表達式以“*”號開始,表明了我們不關心方法返回值的類型。然后,我們指定了全限定類名和方法名。對于方法參數列表,我們使用兩個點號(..)表明切點要選擇任意的perform()方法,無論該方法的入參是什么。
1.用法:
execution( * concert.Performance.perform(..))
2.包含條件時怎么表示呢?
execution( * concert.Performance.perform(..)) && within(concert.*) //表示同時滿足
因為“&”在XML中有特殊含義,所以在Spring的XML配置里面描述切點時,我們可以使用and來代替“&&”。同樣,or 和not可以分別用來代替“||”和“!”。
3.Spring還引入了一個新的bean()指示器,它允許我們在切點表達式中使用bean的ID來標識bean。bean()使用bean ID或bean名稱作為參數來限制切點只匹配特定的bean。:
execution( * concert.Performance.perform(..)) and bean("user") //表示同時滿足
我們還可以使用非操作為除了特定ID以外的其他bean應用通知:
execution( * concert.Performance.perform(..)) and !bean("user") //表示同時滿足
在此場景下,切面的通知會被編織到所有ID不為woodstock的bean中。
二、使用注解創建切面
AspectJ提供了五個注解來定義通知(要實現的切面):
- @After :通知方法會在目標方法返回或拋出異常后調用。
- @AfterReturning : 通知方法會在目標方法返回后調用。
- @AfterThrowing :通知方法會在目標方法拋出異常后調用。
- @Around : 通知方法會將目標方法封裝起來。
- @Before : 通知方法會在目標方法調用之前執行。
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class Audience {
@Before("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void beforeAction(){
System.out.println("beforeAction");
}
@After("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void afterAction(){
System.out.println("afterAction");
}
@AfterReturning("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void afterReturningAction(){
System.out.println("afterReturningAction");
}
@AfterThrowing("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void afterThrowingAction(){
System.out.println("afterThrowingAction");
}
}
我們完全可以這樣做:@Pointcut注解能夠在一個@AspectJ切面內定義可重用的切點,從而減少代碼的重復量。
package com.df.test.service.impl;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
@Pointcut("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void Action(){
}
@Before("Action()")
public void beforeAction(){
System.out.println("beforeAction");
}
@After("Action()")
public void afterAction(){
System.out.println("afterAction");
}
@AfterReturning("Action()")
public void afterReturningAction(){
System.out.println("afterReturningAction");
}
@AfterThrowing("Action()")
public void afterThrowingAction(){
System.out.println("afterThrowingAction");
}
}
光有切面和切點是不夠了,連接點(一個很基礎的類CDPlayer,包含play方法)這里就不展示了。還要講切面注入到Spring容器中去,并使用@EnableAspectJAutoProxy 注解,表示啟用自動代理功能,xml用<aop:aspectj-autoproxy>來啟用自動代理功能。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackageClasses = {SgtPeppers.class})
@EnableAspectJAutoProxy // 啟用自動代理功能
public class CDPlayerConfig {
@Bean
public Audience getAd(){
return new Audience();
}
}
當然,我們也可以使用@Around環繞通知,就可以代替以上所有通知能干的事情:
...
import org.aspectj.lang.ProceedingJoinPoint;
@Aspect
public class Audience {
@Pointcut("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void Action(){
}
@Around("Action()")
public void aroundAction(ProceedingJoinPoint jp) { //必須要這個參數
try {
System.out.println("beforeAction");
jp.proceed(); //執行連接點的方法,也就是play()方法
System.out.println("afterAction");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("afterThrowingAction");
e.printStackTrace();
}
}
}
有時候我們要攔截參數,做一些其他操作,可以這樣完成:
package com.df.test.service.impl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
@Pointcut("execution(* com.df.test.service.impl.CDPlayer.play(..))")
public void Action(){
}
//可能在切點被執行之前,需要對傳入的參數進行處理
@Before("Action() && args(prams3)")
public void beforeAction(String prams3){ //args(prams3)參數名稱必須與形參保持一致
System.out.println("beforeAction" + prams3);
System.out.println("beforeAction");
}
@After("Action()")
public void afterAction(){
System.out.println("afterAction");
}
//對結果進行處理。ps:這里本人實現時,result一直是null,暫時沒弄清楚原因,知道的同學請不吝賜教
@AfterReturning(pointcut = "Action()" ,returning = "result")
public void afterReturningAction(String result){ //returning = "result",名稱要與形參保持一致
System.out.println("afterReturningAction" + result);
}
//要是跑異常,可以攔截起來做處理
@AfterThrowing(pointcut = "Action()" ,throwing = "e")
public void afterThrowingAction(Exception e){
System.out.println("afterThrowingAction" +e.getMessage());
}
}
SpringAOP的基本使用,到這里已經完成了。后面將會繼續探討通過AOP思想所引入的新功能以及AspectJ的使用。