Spring中的AOP面向切面編程詳解
一、什么是AOP
AOP 即面向切面編程,和 OOP(面向?qū)ο缶幊蹋╊愃?,也是一種編程思想。
二、AOP的作用
AOP 采取橫向抽取機(jī)制(動(dòng)態(tài)代理),取代了傳統(tǒng)縱向繼承機(jī)制的重復(fù)性代碼,其應(yīng)用主要體現(xiàn)在事務(wù)處理、日志管理、權(quán)限控制、異常處理等方面。
主要作用是分離功能性需求和非功能性需求,使開發(fā)人員可以集中處理某一個(gè)關(guān)注點(diǎn)或者橫切邏輯,減少對業(yè)務(wù)代碼的侵入,增強(qiáng)代碼的可讀性和可維護(hù)性。
簡單的說,AOP 的作用就是保證開發(fā)者在不修改源代碼的前提下,為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能。AOP 就是代理模式的典型應(yīng)用。
三、AOP中的術(shù)語
- 切面(Aspect):被抽取的公共模塊,可能會(huì)橫切多個(gè)對象。 在Spring AOP中,切面可以使用通用類(基于模式的風(fēng)格) 或者在普通類中以 @AspectJ 注解來實(shí)現(xiàn)。
- 連接點(diǎn)(Join point):指方法,在Spring AOP中,一個(gè)連接點(diǎn) 總是 代表一個(gè)方法的執(zhí)行。
- 通知(Advice):在切面的某個(gè)特定的連接點(diǎn)(Join point)上執(zhí)行的動(dòng)作。通知有各種類型,其中包括“around”、“before”和“after”等通知。許多AOP框架,包括Spring,都是以攔截器做通知模型, 并維護(hù)一個(gè)以連接點(diǎn)為中心的攔截器鏈。
- 切入點(diǎn)(Pointcut):切入點(diǎn)是指 我們要對哪些Join point進(jìn)行攔截的定義。通過切入點(diǎn)表達(dá)式,指定攔截的方法,比如指定攔截add、search。
- 代理(Proxy):代理類 。
- 目標(biāo)(Target): 被一個(gè)或者多個(gè)切面(aspect)所通知(advise)的對象。也有人把它叫做 被通知(adviced) 對象。 既然Spring AOP是通過運(yùn)行時(shí)代理實(shí)現(xiàn)的,這個(gè)對象永遠(yuǎn)是一個(gè) 被代理(proxied) 對象。
- 織入(Weaving):指把增強(qiáng)應(yīng)用到目標(biāo)對象來創(chuàng)建新的代理對象的過程。Spring是在運(yùn)行時(shí)完成織入。
| 名稱 | 說明 |
| Aspect(切面) | 切入點(diǎn)和通知的結(jié)合。 |
| Joinpoint(連接點(diǎn)) | 指那些被攔截到的點(diǎn),在 Spring 中,指可以被動(dòng)態(tài)代理攔截目標(biāo)類的方法。 |
| Advice(通知) | 指攔截到 Joinpoint 之后要做的事情,即對切入點(diǎn)增強(qiáng)的內(nèi)容。 |
| Pointcut(切入點(diǎn)) | 指要對哪些 Joinpoint 進(jìn)行攔截,即被攔截的連接點(diǎn)。 |
| Proxy(代理) | 指生成的代理對象。 |
| Target(目標(biāo)) | 指代理的目標(biāo)對象。 |
| Weaving(植入) | 指把增強(qiáng)代碼應(yīng)用到目標(biāo)上,生成代理對象的過程。 |
四、AOP中的通知類型
- 前置通知(Before): 在目標(biāo)方法被調(diào)用前調(diào)用通知功能;
- 后置通知(After): 在目標(biāo)方法被調(diào)用之后調(diào)用通知功能;
- 返回通知(After-returning): 在目標(biāo)方法成功執(zhí)行之后調(diào)用通知功能;
- 異常通知(After-throwing): 在目標(biāo)方法拋出異常之后調(diào)用通知功能;
- 環(huán)繞通知(Around): 把整個(gè)目標(biāo)方法包裹起來,在被調(diào)用前和調(diào)用之后分別調(diào)用通知功能。
| 通知 | 說明 |
| 前置通知(Before) | 通知方法在目標(biāo)方法調(diào)用之前執(zhí)行 |
| 后置通知(After) | 通知方法在目標(biāo)方法返回或異常后調(diào)用 |
| 返回通知(After-returning) | 通知方法會(huì)在目標(biāo)方法返回后調(diào)用 |
| 異常通知(After-throwing) | 通知方法會(huì)在目標(biāo)方法拋出異常后調(diào)用 |
| 環(huán)繞通知(Around) | 通知方法會(huì)將目標(biāo)方法封裝起來 |
執(zhí)行順序:
①?zèng)]有異常情況下的執(zhí)行順序:
- around before advicez
- before advice
- target method 執(zhí)行
- around after advice
- after advice
- afterReturning
②有異常情況下的執(zhí)行順序:
- around before advice
- before advice
- target method 執(zhí)行
- around after advice
- after advice
- afterThrowing:異常發(fā)生
五、開發(fā)流程
1、導(dǎo)入依賴
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency>
2、applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean id="userAOP" class="com.ape.aop.UserAOP"/>
<bean id="userService" class="com.ape.service.impl.UserServiceImpl"/>
<!-- 配置切面 -->
<aop:config>
<!-- 定義切點(diǎn) -->
<aop:pointcut id="pointcut" expression="execution(* com.ape.service.impl.*.*(..))"/>
<!-- 增強(qiáng) -->
<aop:aspect ref="userAOP">
<!-- 前置通知 -->
<aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
<!-- 后置通知 -->
<aop:after-returning pointcut-ref="pointcut" method="afterReturnAdvice"/>
<!-- 環(huán)繞通知 -->
<aop:around pointcut-ref="pointcut" method="aoundAdvice"/>
<!-- 異常通知 -->
<aop:after-throwing pointcut-ref="pointcut" method="afterThrowAdvice"/>
<!-- 最終通知 -->
<aop:after pointcut-ref="pointcut" method="afterAdvice"/>
</aop:aspect>
</aop:config>
</beans>3、或通過注解實(shí)現(xiàn)
@Aspect
@Component
public class UserAOP {
@Pointcut("execution(* com.ape.service.impl.*.*(..))")
public void pointcut() {
}
@Before("pointcut()")
public void beforeAdvice(JoinPoint point) {
System.out.println("前置增強(qiáng):" + point.getSignature().getName());
}
}六、AOP的優(yōu)點(diǎn)
AOP 是 Spring 的核心之一,在 Spring 中經(jīng)常會(huì)使用 AOP 來簡化編程。在 Spring 框架中使用 AOP 主要有以下優(yōu)勢。
- 提供聲明式企業(yè)服務(wù),特別是作為 EJB 聲明式服務(wù)的替代品。最重要的是,這種服務(wù)是聲明式事務(wù)管理。
- 允許用戶實(shí)現(xiàn)自定義切面。在某些不適合用 OOP 編程的場景中,采用 AOP 來補(bǔ)充。
- 可以對業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)也提高了開發(fā)效率。
到此這篇關(guān)于Spring中的AOP面向切面編程詳解的文章就介紹到這了,更多相關(guān)SpringAOP詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot測試時(shí)卡在Resolving Maven dependencies的問題
這篇文章主要介紹了SpringBoot測試時(shí)卡在Resolving Maven dependencies的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
基于hibernate實(shí)現(xiàn)的分頁技術(shù)實(shí)例分析
這篇文章主要介紹了基于hibernate實(shí)現(xiàn)的分頁技術(shù),結(jié)合實(shí)例形式分析了Hibernate分頁技術(shù)的原理,實(shí)現(xiàn)步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03
Java 實(shí)現(xiàn)并發(fā)的幾種方式小結(jié)
這篇文章主要介紹了Java 實(shí)現(xiàn)并發(fā)的幾種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

