Spring(4)——面向切面編程(AOP模塊)


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 萬能的大腦

歡迎轉載,轉載請注明出處!
簡書ID:@我沒有三顆心臟
github:wmyskxz
歡迎關注公眾微信號:wmyskxz_javaweb
分享自己的Java Web學習之路以及各種Java學習資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM