Spring AOP 簡介 如果說 IoC 是 Spring 的核心,那麼面向切麵編程就是 Spring 最為重要的功能之一了,在資料庫事務中切麵編程被廣泛使用。 AOP 即 Aspect Oriented Program 面向切麵編程 首先,在面向切麵編程的思想裡面,把功能分為核心業務功能,和周邊 ...
Spring AOP 簡介
如果說 IoC 是 Spring 的核心,那麼面向切麵編程就是 Spring 最為重要的功能之一了,在資料庫事務中切麵編程被廣泛使用。
AOP 即 Aspect Oriented Program 面向切麵編程
首先,在面向切麵編程的思想裡面,把功能分為核心業務功能,和周邊功能。
- 所謂的核心業務,比如登陸,增加數據,刪除數據都叫核心業務
- 所謂的周邊功能,比如性能統計,日誌,事務管理等等
周邊功能在 Spring 的面向切麵編程AOP思想里,即被定義為切麵
在面向切麵編程AOP的思想裡面,核心業務功能和切麵功能分別獨立進行開發,然後把切麵功能和核心業務功能 "編織" 在一起,這就叫AOP
AOP 的目的
AOP能夠將那些與業務無關,卻為業務模塊所共同調用的邏輯或責任(例如事務處理、日誌管理、許可權控制等)封裝起來,便於減少系統的重覆代碼,降低模塊間的耦合度,並有利於未來的可拓展性和可維護性。
AOP 當中的概念:
- 切入點(Pointcut)
在哪些類,哪些方法上切入(where) - 通知(Advice)
在方法執行的什麼實際(when:方法前/方法後/方法前後)做什麼(what:增強的功能) - 切麵(Aspect)
切麵 = 切入點 + 通知,通俗點就是:在什麼時機,什麼地方,做什麼增強! - 織入(Weaving)
把切麵加入到對象,並創建出代理對象的過程。(由 Spring 來完成)
一個例子
為了更好的說明 AOP 的概念,我們來舉一個實際中的例子來說明:
在上面的例子中,包租婆的核心業務就是簽合同,收房租,那麼這就夠了,灰色框起來的部分都是重覆且邊緣的事,交給中介商就好了,這就是 AOP 的一個思想:讓關註點代碼與業務代碼分離!
實際的代碼
我們來實際的用代碼感受一下
1.在 Package【pojo】下新建一個【Landlord】類(我百度翻譯的包租婆的英文):
package pojo;
import org.springframework.stereotype.Component;
@Component("landlord")
public class Landlord {
public void service() {
// 僅僅只是實現了核心的業務功能
System.out.println("簽合同");
System.out.println("收房租");
}
}
2.在 Package【aspect】下新建一個中介商【Broker】類(我還是用的翻譯...):
package aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
@Before("execution(* pojo.Landlord.service())")
public void before(){
System.out.println("帶租客看房");
System.out.println("談價格");
}
@After("execution(* pojo.Landlord.service())")
public void after(){
System.out.println("交鑰匙");
}
}
3.在 applicationContext.xml 中配置自動註入,並告訴 Spring IoC 容器去哪裡掃描這兩個 Bean:
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="aspect" />
<context:component-scan base-package="pojo" />
<aop:aspectj-autoproxy/>
</beans>
4.在 Package【test】下編寫測試代碼:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Landlord;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Landlord landlord = (Landlord) context.getBean("landlord", Landlord.class);
landlord.service();
}
}
5.執行看到效果:
這個例子使用了一些註解,現在看不懂沒有關係,但我們可以從上面可以看到,我們在 Landlord 的 service() 方法中僅僅實現了核心的業務代碼,其餘的關註點功能是根據我們設置的切麵自動補全的。
使用註解來開發 Spring AOP
使用註解的方式已經逐漸成為了主流,所以我們利用上面的例子來說明如何用註解來開發 Spring AOP
第一步:選擇連接點
Spring 是方法級別的 AOP 框架,我們主要也是以某個類額某個方法作為連接點,另一種說法就是:選擇哪一個類的哪一方法用以增強功能。
....
public void service() {
// 僅僅只是實現了核心的業務功能
System.out.println("簽合同");
System.out.println("收房租");
}
....
我們在這裡就選擇上述 Landlord 類中的 service() 方法作為連接點。
第二步:創建切麵
選擇好了連接點就可以創建切麵了,我們可以把切麵理解為一個攔截器,當程式運行到連接點的時候,被攔截下來,在開頭加入了初始化的方法,在結尾也加入了銷毀的方法而已,在 Spring 中只要使用 @Aspect
註解一個類,那麼 Spring IoC 容器就會認為這是一個切麵了:
package aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
@Before("execution(* pojo.Landlord.service())")
public void before(){
System.out.println("帶租客看房");
System.out.println("談價格");
}
@After("execution(* pojo.Landlord.service())")
public void after(){
System.out.println("交鑰匙");
}
}
- 註意: 被定義為切麵的類仍然是一個 Bean ,需要
@Component
註解標註
代碼部分中在方法上面的註解看名字也能猜出個大概,下麵來列舉一下 Spring 中的 AspectJ 註解:
註解 | 說明 |
---|---|
@Before |
前置通知,在連接點方法前調用 |
@Around |
環繞通知,它將覆蓋原有方法,但是允許你通過反射調用原有方法,後面會講 |
@After |
後置通知,在連接點方法後調用 |
@AfterReturning |
返回通知,在連接點方法執行並正常返回後調用,要求連接點方法在執行過程中沒有發生異常 |
@AfterThrowing |
異常通知,當連接點方法異常時調用 |
有了上表,我們就知道 before() 方法是連接點方法調用前調用的方法,而 after() 方法則相反,這些註解中間使用了定義切點的正則式,也就是告訴 Spring AOP 需要攔截什麼對象的什麼方法,下麵講到。
第三步:定義切點
在上面的註解中定義了 execution 的正則表達式,Spring 通過這個正則表達式判斷具體要攔截的是哪一個類的哪一個方法:
execution(* pojo.Landlord.service())
依次對這個表達式作出分析:
- execution:代表執行方法的時候會觸發
*
:代表任意返回類型的方法- pojo.Landlord:代表類的全限定名
- service():被攔截的方法名稱
通過上面的表達式,Spring 就會知道應該攔截 pojo.Lnadlord 類下的 service() 方法。上面的演示類還好,如果多出都需要寫這樣的表達式難免會有些複雜,我們可以通過使用 @Pointcut
註解來定義一個切點來避免這樣的麻煩:
package aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
@Pointcut("execution(* pojo.Landlord.service())")
public void lService() {
}
@Before("lService()")
public void before() {
System.out.println("帶租客看房");
System.out.println("談價格");
}
@After("lService()")
public void after() {
System.out.println("交鑰匙");
}
}
第四步:測試 AOP
編寫測試代碼,但是我這裡因為 JDK 版本不相容出現了 BUG....(尷尬...)
這就告訴我們:環境配置很重要...不然莫名其妙的 BUG 讓你崩潰...
環繞通知
我們來探討一下環繞通知,這是 Spring AOP 中最強大的通知,因為它集成了前置通知和後置通知,它保留了連接點原有的方法的功能,所以它及強大又靈活,讓我們來看看:
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
class Broker {
// 註釋掉之前的 @Before 和 @After 註解以及對應的方法
// @Before("execution(* pojo.Landlord.service())")
// public void before() {
// System.out.println("帶租客看房");
// System.out.println("談價格");
// }
//
// @After("execution(* pojo.Landlord.service())")
// public void after() {
// System.out.println("交鑰匙");
// }
// 使用 @Around 註解來同時完成前置和後置通知
@Around("execution(* pojo.Landlord.service())")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("帶租客看房");
System.out.println("談價格");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("交鑰匙");
}
}
運行測試代碼,結果仍然正確:
使用 XML 配置開發 Spring AOP
註解是很強大的東西,但基於 XML 的開發我們仍然需要瞭解,我們先來瞭解一下 AOP 中可以配置的元素:
AOP 配置元素 | 用途 | 備註 |
---|---|---|
aop:advisor |
定義 AOP 的通知其 | 一種很古老的方式,很少使用 |
aop:aspect |
定義一個切麵 | —— |
aop:before |
定義前置通知 | —— |
aop:after |
定義後置通知 | —— |
aop:around |
定義環繞通知 | —— |
aop:after-returning |
定義返回通知 | —— |
aop:after-throwing |
定義異常通知 | —— |
aop:config |
頂層的 AOP 配置元素 | AOP 的配置是以它為開始的 |
aop:declare-parents |
給通知引入新的額外介面,增強功能 | —— |
aop:pointcut |
定義切點 | —— |
有了之前通過註解來編寫的經驗,並且有了上面的表,我們將上面的例子改寫成 XML 配置很容易(去掉所有的註解):
<!-- 裝配 Bean-->
<bean name="landlord" class="pojo.Landlord"/>
<bean id="broker" class="aspect.Broker"/>
<!-- 配置AOP -->
<aop:config>
<!-- where:在哪些地方(包.類.方法)做增加 -->
<aop:pointcut id="landlordPoint"
expression="execution(* pojo.Landlord.service())"/>
<!-- what:做什麼增強 -->
<aop:aspect id="logAspect" ref="broker">
<!-- when:在什麼時機(方法前/後/前後) -->
<aop:around pointcut-ref="landlordPoint" method="around"/>
</aop:aspect>
</aop:config>
運行測試程式,看到正確結果:
擴展閱讀:Spring【AOP模塊】就這麼簡單 、 關於 Spring AOP(AspectJ)你該知曉的一切(慎獨讀,有些深度...)
參考資料:
- 《Java EE 互聯網輕量級框架整合開發》
- 《Java 實戰(第四版)》
- 萬能的百度 and 萬能的大腦
歡迎轉載,轉載請註明出處!
@我沒有三顆心臟
CSDN博客:http://blog.csdn.net/qq939419061
簡書:http://www.jianshu.com/u/a40d61a49221