Spring框架學習與實踐(十)

Spring 通知類型及使用 ProxyFactoryBean 創建AOP代理演練

Spring 通知按照在目標類方法的連接點位置,可以分爲以下五種類型,如表:

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

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

1. 導入 JAR 包

在覈心 JAR 包的基礎上,再向 springDemo03 項目的 lib 目錄中導入 AOP 的 JAR 包,具體如下:

1)spring-aop-3.2.13.RELEASE.jar:是 Spring 爲 AOP 提供的實現,在 Spring 的包中已經提供

2)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 {

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		// TODO Auto-generated method stub
		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 測試運行 FactoryBeanTest,運行成功後,控制檯的輸出結果如圖:

 

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