前言
前面已經講解了
bean
的裝配技術,接着學習Spring
中另外一個核心概念:切面
。
面向切面
面向切面編程
切面能夠幫助模塊化
橫切關注點
,橫切關注點
可以被描述為影響應用的功能,如為業務添加安全和事務管理等。
AOP(Aspect Orient Programming)
- 通知,通知定義切面何時被使用,
Spring
切面可以應用5
種類型的通知。- 前置通知(Before),在目標方法被調用之前調用通知功能。
- 后置通知(After),在目標方法完成之后調用通知,並不關心方法的輸出。
- 返回通知(AfterReturning),在目標方法成功執行之后調用通知。
- 異常通知(AfterThrowing),在目標方法拋出異常后調用通知。
- 環形通知(Around),通知包裹了被通知的方法,在被通知的方法調用之前和調用之后執行自定義的行為。
- 連接點,在應用執行過程中能夠插入切面的一個點。
- 切點,匹配通知所要織入的一個或多個連接點。
- 切面,通知和切點的結合。
- 引入,允許向現有類添加新方法或屬性。
- 織入,把切面應用到目標對象並創建新的代理對象的過程,切面可以在指定的連接點被織入到目標對象中,在目標對象的生命周期中有多個點可以進行織入。
- 編譯期,在目標類編譯時被織入,需要特殊的編譯器支持。
- 類加載器,切面在目標類加載到
JVM
時被織入,需要特殊類加載器。 - 運行期,在應用運行的某個時刻被織入,
AOP
容器會為目標對象動態創建代理對象,這也是Spring AOP
的織入方式。
Spring AOP
Spring
對AOP
的支持在很多方面借鑒了AspectJ
項目,提供如下四種支持。
- 基於代理的經典
Spring AOP
。 - 純
POJO
切面。 @AspectJ
注解的切面。- 注入式
AspectJ
切面(適用於Spring
各版本)。
Spring AOP
構建在動態代理基礎上,只能局限於對方法攔截;Spring
在運行時通知對象(通過在代理類中包裹切面,在運行期把切面織入到Spring
管理的bean
中,代理類封裝了目標類,並攔截被通知方法的調用,執行切面邏輯,再把調用轉發給真正的目標bean
);Spring
只支持方法級別的連接點(基於動態代理決定)。
通過切點選擇連接點
編寫切點
首先先定義一個方法
package ch4
public interface Performance {
void perform();
}
然后使用切點表達式設置當
perform
方法執行時觸發通知的調用execution(* ch4.Performance.perform(..)),*表示並不關心返回值,然后指定具體的方法名,方法中的..
表示切點要選擇任意的perform
方法。還可使用&&、and、||、or
對切點進行限定。
切點中選擇bean
切點表達式中可使用
bean
的ID
來標識bean
,如下切點表達式execution(* ch4.Performance.perform(..)) && bean(musicPerformance),表示限定beanID
為musicPerformance
時調用通知,其中musicPerformance
是Performance
的一個子類實現。
使用注解創建切面
定義切面
定義一個切面如下。
package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
@Before("execution(* ch4.Performance.perform(..))")
public void silenceCellPhones() {
System.out.println("Silencing cell phones");
}
@Before("execution(* ch4.Performance.perform(..))")
public void takeSeats() {
System.out.println("Taking seats");
}
@AfterReturning("execution(* ch4.Performance.perform(..))")
public void applause() {
System.out.println("CLAP CLAP CLAP CLAP");
}
@AfterThrowing("execution(* ch4.Performance.perform(..))")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}
可以看到配合注解和切點表達式可以使得在執行
perform
方法之前、之后完成指定動作,當然,對於每個方法都使用了execution
切點表達式,可以進一步進行精簡。
package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
@Pointcut("execution(* ch4.Performance.perform(..))")
public void performance() {
}
@Before("performance()")
public void silenceCellPhones() {
System.out.println("Silencing cell phones");
}
@Before("performance()")
public void takeSeats() {
System.out.println("Taking seats");
}
@AfterReturning("performance()")
public void applause() {
System.out.println("CLAP CLAP CLAP CLAP");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}
可以看到使用
@Pointcut
定義切點,然后在其他方法中直接使用注解和切點方法即可,不需要再繁瑣的使用execution
切點表達式。
啟動代理功能
在定義了注解后,需要啟動,否則無法識別,啟動方法分為在
JavaConfig
中顯式配置和XML
注解。
- JavaConfig顯式配置
package ch4;
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
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig {
@Bean
public Audience audience() {
return new Audience();
}
}
- XML配置
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="ch4"/>
</beans>
創建環繞通知
將被通知的目標方法完全包裝起來,就像在一個通知方法中同時編寫前置通知和后置通知。
package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
@Pointcut("execution(* ch4.Performance.perform(..)) && ! bean(musicPerformance)")
public void performance() {
}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP CLAP");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
使用
Around
注解表示環繞通知,注意需要調用proceed()
方法來調用實際的通知方法。
處理通知中的參數
在
perform
方法中添加int number
參數表示有多少觀眾,使用如下切點表達式execution(\* ch4.Performance.perform(int)) && args(number)
,表示需要匹配perform(int)
型方法並且通知方法的參數名為number
。
MusicPerformance
如下
package ch4;
import org.springframework.stereotype.Service;
@Service
public class MusicPerformance implements Performance {
public void perform(int number) {
System.out.println("perform music, and the audience number is " + number);
}
}
Audience
如下
package ch4;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Audience {
@Pointcut("execution(* ch4.Performance.perform(int)) && args(number)")
public void performance(int number) {
}
@Before("performance(int)")
public void silenceCellPhones() {
System.out.println("Silencing cell phones");
}
@Before("performance(int)")
public void takeSeats() {
System.out.println("Taking seats");
}
@AfterReturning("performance(int)")
public void applause() {
System.out.println("CLAP CLAP CLAP CLAP");
}
@AfterThrowing("performance(int)")
public void demandRefund() {
System.out.println("Demanding a refund");
}
@Around("performance(int)")
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP CLAP");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
- 測試
AOPTest
如下
package ch4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring-learning.xml")
public class AOPTest {
@Autowired
private Performance performance;
@Test
public void notNull() {
assertNotNull(performance);
performance.perform(100);
System.out.println("++++++++++++++++++");
performance.perform(999);
System.out.println("++++++++++++++++++");
}
}
運行結果:
Silencing cell phones
Taking seats
Taking seats
Silencing cell phones
perform music, and the audience number is 100
CLAP CLAP CLAP CLAP
CLAP CLAP CLAP CLAP
++++++++++++++++++
Silencing cell phones
Taking seats
Taking seats
Silencing cell phones
perform music, and the audience number is 999
CLAP CLAP CLAP CLAP
CLAP CLAP CLAP CLAP
++++++++++++++++++
在XML中聲明切面
除了使用注解方式聲明切面外,還可通過
XML
方式聲明切面。
前置通知和后置通知
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="ch4"/>
<aop:config>
<aop:aspect ref="audience">
<aop:before
pointcut="execution(* ch4.Performance.perform(..))"
method="silenceCellPhones" />
<aop:before
pointcut="execution(* ch4.Performance.perform(..))"
method="takeSeats" />
<aop:after-returning
pointcut="execution(* ch4.Performance.perform(..))"
method="applause" />
<aop:after-throwing
pointcut="execution(* ch4.Performance.perform(..))"
method="demandRefund" />
</aop:aspect>
</aop:config>
</beans>
將
Audience
注解刪除后運行單元測試可得出正確結果;當然上述XML
也有點復雜,可進一步簡化。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="ch4"/>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut
id="performance"
expression="execution(* ch4.Performance.perform(..))" />
<aop:before
pointcut-ref="performance"
method="silenceCellPhones" />
<aop:before
pointcut-ref="performance"
method="takeSeats" />
<aop:after-returning
pointcut-ref="performance"
method="applause" />
<aop:after-throwing
pointcut-ref="performance"
method="demandRefund" />
</aop:aspect>
</aop:config>
</beans>
聲明環繞通知
XML如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="ch4"/>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut
id="performance"
expression="execution(* ch4.Performance.perform(..))"/>
<aop:around
pointcut-ref="performance"
method="watchPerformance" />
</aop:aspect>
</aop:config>
</beans>
運行單元測試,可得正確結果。
為通知傳遞參數
XML文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="ch4"/>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut
id="performance"
expression="execution(* ch4.Performance.perform(int)) and args(int)" />
<aop:before
pointcut-ref="performance"
method="silenceCellPhones" />
<aop:before
pointcut-ref="performance"
method="takeSeats" />
<aop:after-returning
pointcut-ref="performance"
method="applause" />
<aop:after-throwing
pointcut-ref="performance"
method="demandRefund" />
</aop:aspect>
</aop:config>
</beans>
運行單元測試,可得正確結果。
總結
AOP
是Spring
的核心概念,通過AOP
,我們可以把切面插入到方法執行的周圍,通過本篇博文可以大致了解AOP
的使用方法。源碼已經上傳至github,歡迎fork and star
。