天天看點

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學習資料

繼續閱讀