Spring筆記——AOP(XML方式)

相比於註解方法,使用XML配置方式有如下幾個優點:
☞ 如果應用沒有使用1.5以上的JDK版本,那麼應用只能使用XML配置方式來管理切面、切入點和增強處理等
☞ 採用XML配置方式時,對早期的Spring用戶來說更加習慣,而且這種方式允許使用純粹的POJO來支持AOP。當使用AOP作爲工具來配置企業服務時,XML會是一個很好的選擇。當使用XML風格時,我們可以在配置文件中清晰的看出系統中存在哪些切面。

使用XML配置方式,則存在如下幾個缺點:
☞ 使用XML配置方式不能將切面、切入點、增強處理等封裝到一個地方。當我們需要查看切面、切入點、增強處理時,必須同時結合Java文件和XML配置文件來查看;但使用@AspectJ時,則只需一個單獨的類文件即可看到切面、切入點和增強處理的全部信息
☞ XML配置方式比@AspectJ方式有更多的限制:僅支持”singleton”切面Bean,不能在XML中組合多個命名連接點的聲明

在Spring配置文件中,所有切面、切入點和增強處理都必須定義在<aop:config.../>元素內部。<beans.../>元素下可以包含多個<aop:config.../>元素,一個<aop:config.../>可以包含pointcut、advisor和aspect元素,且這三個元素必須按照此順序來定義

1.配置切面

定義切面使用<aop:aspect.../>元素,使用該元素來定義切面時,其實質是將一個已有的Spring Bean轉換成切面Bean,所以需要先定義一個普通的Spring Bean。
配置<aop:aspect.../>元素時可以指定如下是三個屬性:
☞ id:定義該切面的標識名
☞ ref:指定以指定ref屬性所引用的普通Bean作爲切面Bean
☞ order:指定該切面Bean的優先級,該屬性的作用與前面@AspectJ中@Order、Order接口作用完全一樣,order屬性值越小,該切面對應的優先級越高

如下配置片段定義了一個切面:

<aop:config>
    <!-- 將容器中的afterAdviceBean轉換成切面Bean,切面Bean的新名稱爲afterAdviceAspect -->
    <aop:aspect id="afterAdviceAspect" ref="afterAdviceBean">
        ...
    </aop:aspect>
</aop:config>
<!-- 定義一個普通Bean實例,該Bean實例將作爲Aspect Bean -->
<bean id="afterAdviceBean" class="main.java.aspect.AfterAdviceTest"></bean>

2.配置增強處理

與使用@AspectJ完全一樣,使用XML一樣可以配置Before、After、AfterReturning、AfterThrowing和Around5種增強處理,而且完全支持和@Aspect完全一樣的語義
使用XML配置增強處理分別依賴於如下幾個元素:
<aop:before.../>:配置before增強處理
<aop:after.../>:配置after增強處理
<aop:after-returning.../>:配置after-returning增強處理
<aop:after-throwing.../>:配置after-throwing增強處理
<aop:around.../>:配置around增強處理

上面這些元素都不支持使用子元素,但通常可以指定如下屬性。
☞ pointcut:概屬性指定一個切入表達式,Spring將在匹配該表達式的連接點時織入該增強處理
☞ pointcut-ref:該屬性指定一個已經存在的切入點名稱,通常pointcut和pointcut-ref兩個屬性只需要使用其中之一
☞ method:該屬性指定一個方法名,指定切面Bean的該方法將作爲增強處理
☞ throwing:該屬性只對<aop:after-throwing.../>元素有效,用於指定一個形參名,AfterThrowing增強處理方法可通過該形參訪問目標方法所拋出的異常
☞ returning:該屬性只對<aop:after-returning.../>元素有效,用於指定一個形參名,AfterReturuning增強處理方法可通過該形參訪問目標方法的返回值

XML配置方式和@AspectJ方式一樣支持組合切入點表達式,但XML配置方式不再使用簡單的&&、||和!作爲組合運算,而是使用如下三個組合運算符:and、or、和not

public class FourAdviceTest {
    // 定義Around增強處理
    public Object processTx(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("Around增強:執行目標方法之前,模擬開始事物...");
        // 訪問執行目標方法的參數
        Object[] args = jp.getArgs();
        // 當執行目標方法的參數存在
        // 且第一個參數是字符串參數
        if (args != null && args.length > 0 && args[0].getClass() == String.class) {
            // 改變第一個目標方法的第一個參數
            args[0] = "被改變的參數";
        }
        // 執行目標方法,並保存目標方法執行後的返回值
        Object rvt = jp.proceed(args);
        System.out.println("Around增強:執行目標方法之前,模擬結束事物...");
        return rvt + "新曾的內容";
    }

    // 定義Bfore增強處理
    public void authority(JoinPoint jp) {
        System.out.println("Before增強:模擬執行權限檢查");
        // 返回被織入增強處理的目標方法
        System.out.println("Before增強處理:被織入增強處理的目標方法爲:" + jp.getSignature().getName());
        // 訪問執行目標方法的參數
        System.out.println("Before增強:目標方法的參數爲:" + Arrays.toString(jp.getArgs()));
        System.out.println("Before增強:被織入增強處理的目標對象爲:" + jp.getTarget());
    }

    // 定義AfterReturning增強處理
    public void log(JoinPoint jPoint, Object rvt) {
        System.out.println("AfterReturning增強:獲取目標方法返回值:" + rvt);
        System.out.println("AfterReturning增強:模擬日誌記錄功能...");
        // 返回被織入增強處理的目標方法
        System.out.println("AfterReturning增強:被織入增強處理的目標方法爲:" + jPoint.getSignature().getName());
        // 訪問執行目標方法的參數
        System.out.println("AfterReturning增強:目標方法的參數爲:" + Arrays.toString(jPoint.getArgs()));
        // 訪問被增強處理的目標對象
        System.out.println("AfterReturning增強:被織入增強處理的目標對象爲:" + jPoint.getTarget());
    }

    public void release(JoinPoint jp) {
        System.out.println("After增強:模擬方法結束後的釋放資源...");
        // 被返回織入增強處理的目標方法
        System.out.println("After增強:被織入增強處理的目標方法爲:" + jp.getSignature().getName());
        // 訪問執行目標方法的參數
        System.out.println("After增強:目標方法的參數爲:" + Arrays.toString(jp.getArgs()));
        // 訪問被增強處理的目標對象
        System.out.println("After增強:被織入增強處理的目標對象爲:" + jp.getTarget());
    }
}
public class SecondAdviceTest {
    // 定義Before增強處理
    public void authority(String aa) {
        System.out.println("目標方法的參數爲:" + aa);
        System.out.println("①號Before增強:模擬執行權限檢查");
    }
}
<?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:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <aop:config>
        <!-- 將的fourAdviceBean轉換成切面Bean,切面Bean的新名稱爲fourAdviceAspect,指定該切面的優先級爲2 -->
        <aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">
            <!-- 定義一個After增強處理,直接指定切入點表達式,以切面Bean中的release()方法作爲增強處理方法 -->
            <aop:after pointcut="execution(* main.java.service.*.*(..))" method="release" />
            <!-- 定義一個Before增強處理,直接指定切入點表達式,以切面Bean中的authority()方法作爲增強處理方法 -->
            <aop:before pointcut="execution(* main.java.service.*.*(..))" method="authority" />
            <!-- 定義一個AfterReturning增強處理, 直接指定切入點表達式,以切面Bean中的log()方法作爲增強處理方法 -->
            <aop:after-returning pointcut="execution(* main.java.service.*.*(..))" method="log" returning="rvt" />
            <!-- 定義一個Around增強處理, 直接指定切入點表達式,以切面Bean中的processTx()方法作爲增強處理方法 -->
            <aop:around pointcut="execution(* main.java.service.*.*(..))" method="processTx" />
        </aop:aspect>

        <!-- 將secondAdviceBean轉換成切面Bean,切面Bean的新名稱爲:secondAdviceAspect,指定該切面的優先級爲1,改切面裏的增強處理將被優先織入 -->
        <aop:aspect id="secondAdviceAspect" ref="secondAdviceBean" order="1">
            <!-- 定義一個Before增強處理,直接指定切入點表達式,以切面Bean中的authority()方法作爲增強處理方法,且該參數必須爲String類型 -->
            <aop:before pointcut="execution(* main.java.service.*.*(..)) and args(aa)" method="authority"/>
        </aop:aspect>
    </aop:config>
    <!-- 定義一個普通組件Bean -->
    <bean id="chinese" class="main.java.service.impl.Chinese"/>
    <!-- 定義一個普通Bean實例,該Bean實例將被作爲Aspect Bean -->
    <bean id="fourAdviceBean" class="main.java.aspect.FourAdviceTest" />
    <!-- 再定義一個普通Bean實例,該Bean實例將被作爲Aspect Bean -->
    <bean id="secondAdviceBean" class="main.java.aspect.SecondAdviceTest" />
</beans>

3.配置切入點

Spring提供了<aop:pointcut.../>元素來定義切入點。當把<aop:pointcut.../>作爲<aop:config.../>的子元素定義時,表明該切入點可被多個切面共享;當把<aop:pointcut.../>作爲<aop:aspect.../>的子元素定義時,表明該切入點只能在對應的切面中有效。
配置<aop:pointcut.../>元素時通常需要指定如下兩個屬性:
☞ id:指定該切入點的標識名
☞ expression:指定該切入點關聯的切入點表達式

如下配置片段定義了一個簡單的切入點:

<!-- 定義一個簡單切入點 -->
<aop:pointcut id="myPointcut" expression="execution(* main.java.service.*.*(..))" />

上面的配置片段既可作爲<aop:config.../>的子元素,用於配置全局切入點;也可作爲<aop:aspect.../>的子元素,用於配置僅對該切面有效的切入點。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章