Spring——面向切面編程(AOP模塊)
Spring AOP 簡介
如果說 IoC 是 Spring 的核心,那麼面向切面編程就是 Spring 最爲重要的功能之一了,在數據庫事務中切面編程被廣泛使用。
AOP
AOP即 Aspect Oriented Program 面向切面編程,是對OOP即面向對象的程序設計和POP面向過程的程序設計的補充。
首先,在面向切面編程的思想裏面,把功能分爲核心業務功能,和周邊功能。
所謂的核心業務,比如登陸,增加數據,刪除數據都叫核心業務
所謂的周邊功能,比如性能統計,日誌,事務管理等等
周邊功能在 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 |
前置通知,在連接點方法前調用 |
@After |
後置通知,在連接點方法後調用 |
@Around |
環繞通知,它將覆蓋原有方法,但是允許你通過反射調用原有方法,後面會講 |
@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("交鑰匙");
}
}
環繞通知
我們來探討一下環繞通知,這是 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("交鑰匙");
}
}
運行測試代碼,結果仍然正確:
切面的優先級
- 如果同一個切面類中有多個同一類型的通知(比如前置通知),按照通知的定義順序執行。
- 如果有多個切面類,則可以通過設置切面類的order進行設置優先級,注意:order的數值越小越先執行。
作者:我沒有三顆心臟
鏈接:https://www.jianshu.com/p/994027425b44
來源:簡書