1. 程式人生 > 程式設計 >Spring基於ProxyFactoryBean建立AOP代理

Spring基於ProxyFactoryBean建立AOP代理

Spring 通知型別

通過前面的學習可以知道,通知(Advice)其實就是對目標切入點進行增強的內容,Spring AOP 為通知(Advice)提供了 org.aopalliance.aop.Advice 介面。

Spring 通知按照在目標類方法的連線點位置,可以分為以下五種型別,如表 1 所示。

表 1 Spring 通知的 5 種類型

名稱 說明
org.springframework.aop.MethodBeforeAdvice(前置通知) 在方法之前自動執行的通知稱為前置通知,可以應用於許可權管理等功能。
org.springframework.aop.AfterReturningAdvice(後置通知) 在方法之後自動執行的通知稱為後置通知,可以應用於關閉流、上傳檔案、刪除臨時檔案等功能。
org.aopalliance.intercept.MethodInterceptor(環繞通知) 在方法前後自動執行的通知稱為環繞通知,可以應用於日誌、事務管理等功能。
org.springframework.aop.ThrowsAdvice(異常通知) 在方法丟擲異常時自動執行的通知稱為異常通知,可以應用於處理異常記錄日誌等功能。
org.springframework.aop.IntroductionInterceptor(引介通知) 在目標類中新增一些新的方法和屬性,可以應用於修改舊版本程式(增強類)。

宣告式 Spring AOP

Spring 建立一個 AOP 代理的基本方法是使用 org.springframework.aop.framework.ProxyFactoryBean,這個類對應的切入點和通知提供了完整的控制能力,並可以生成指定的內容。

ProxyFactoryBean 類中的常用可配置屬性如表 2 所示。

表 2 ProxyFactoryBean 的常用屬性

屬性名稱 描 述
target 代理的目標物件
proxyInterfaces 代理要實現的介面,如果有多個介面,則可以使用以下格式賦值:
<list>
<value ></value>
...
</list>
proxyTargetClass 是否對類代理而不是介面,設定為 true 時,使用 CGLIB 代理
interceptorNames 需要植入目標的 Advice
singleton 返回的代理是否為單例,預設為 true(返回單例項)
optimize 當設定為 true 時,強制使用 CGLIB

在 Spring 通知中,環繞通知是一個非常典型的應用。下面通過環繞通知的案例演示 Spring 建立 AOP 代理的過程。

1. 匯入 JAR 包

在核心 JAR 包的基礎上,再向 springDemo03 專案的 lib 目錄中匯入 AOP 的 JAR 包,具體如下。

  • spring-aop-3.2.13.RELEASE.jar:是 Spring 為 AOP 提供的實現,在 Spring 的包中已經提供。
  • com.springsource.org.aopalliance-1.0.0.jar:是 AOP 提供的規範,可以在 Spring 的官網網址https://repo.spring.io/webapp/#/search/quick/ 中進行搜尋並下載。

2. 建立切面類 MyAspect

在 src 目錄下建立一個名為 com.mengma.factorybean 的包,在該包下建立切面類 MyAspect,如下所示。

package com.mengma.factorybean;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

//需要實現介面,確定哪個通知,及告訴Spring應該執行哪個方法
public class MyAspect implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println("方法執行之前");
// 執行目標方法
Object obj = mi.proceed();
System.out.println("方法執行之後");
return obj;
}
}

上述程式碼中,MyAspect 類實現了 MethodInterceptor 介面,並實現了介面的 invoke() 方法。MethodInterceptor 介面是 Spring AOP 的 JAR 包提供的,而 invoke() 方法用於確定目標方法 mi,並告訴 Spring 要在目標方法前後執行哪些方法,這裡為了演示效果在目標方法前後分別向控制檯輸出了相應語句。

3. 建立 Spring 配置檔案

在 com.mengma.factorybean 包下建立配置檔案 applicationContext.xml,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">
  <!--目標類 -->
  <bean id="customerDao" class="com.mengma.dao.CustomerDaoImpl" />
  <!-- 通知 advice -->
  <bean id="myAspect" class="com.mengma.factorybean.MyAspect" />
  <!--生成代理物件 -->
  <bean id="customerDaoProxy"
   class="org.springframework.aop.framework.ProxyFactoryBean">
   <!--代理實現的介面 -->
    <property name="proxyInterfaces" value="com.mengma.dao.CustomerDao" />
    <!--代理的目標物件 -->
    <property name="target" ref="customerDao" />
    <!--用通知增強目標 -->
    <property name="interceptorNames" value="myAspect" />
    <!-- 如何生成代理,true:使用cglib; false :使用jdk動態代理 -->
    <property name="proxyTargetClass" value="true" />
  </bean>
</beans>

上述程式碼中,首先配置目標類和通知,然後使用 ProxyFactoryBean 類生成代理物件;第 14 行程式碼配置了代理實現的介面;第 16 行程式碼配置了代理的目標物件;第 18 行程式碼配置了需要植入目標的通知;當第 20 行程式碼中的 value 屬性值為 true 時,表示使用 CGLIB 代理,屬性值為 false 時,表示使用 JDK 動態代理。

4. 建立測試類

在 com.mengma.factorybean 包下建立一個名為 FactoryBeanTest 的測試類,編輯後如下所示。

package com.mengma.factorybean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mengma.dao.CustomerDao;
public class FactoryBeanTest {
@Test
public void test() {
String xmlPath = "com/mengma/factorybean/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
CustomerDao customerDao = (CustomerDao) applicationContext
.getBean("customerDaoProxy");
customerDao.add();
customerDao.update();
customerDao.delete();
customerDao.find();
}
}

5. 執行專案並檢視結果

使用 JUnit 測試執行 test() 方法,執行成功後,控制檯的輸出結果如圖 1 所示。

圖 1 執行結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。