Spring中基於配置文件的方式來配置AOP

Spring中基於配置文件的方式來配置AOP

一、說在前面

除了使用 AspectJ 註解聲明切面,Spring 也支持在 Bean 配置文件中聲明切面:
1、當使用 XML 聲明切面時,需要在 <beans> 根元素中導入 aop Schema。
2、在 Bean 配置文件中,所有的 Spring AOP 配置都必須定義在 <aop:config> 元素內部。對於每個切面而言,都要創建一個 <aop:aspect>元素來爲具體的切面實現引用後端Bean 實例。
3、切面 Bean 必須有一個標示符,供 <aop:aspect> 元素引用。
4、切入點使用 <aop:pointcut> 元素聲明。
5、切入點必須定義在 <aop:aspect> 元素下,或者直接定義在 <aop:config> 元素下:
(1)定義在 <aop:aspect> 元素下: 只對當前切面有效;
(2)定義在 <aop:config> 元素下: 對所有切面都有效。
6、基於 XML 的 AOP 配置不允許在切入點表達式中用名稱引用其他切入點。

二、實例代碼如下

1、ArithmeticCalculator接口

package com.at.aop.xml;

public interface ArithmeticCalculator {

	int add(int i,int j);
	int sub(int i,int j);
	
	int mul(int i,int j);
	int div(int i,int j);
}
2、ArithmeticCalculatorImpl實現類

package com.at.aop.xml;


public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

}
3、LoggingAspect類

package com.at.aop.xml;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class LoggingAspect {

	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println("前置通知方法 "+methodName+" 開始 "+args);
	}
	
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("後置通知方法 "+methodName+" 結束 ");
	}
	
	public void afterReturning(JoinPoint joinPoint,Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("返回通知方法 "+methodName+" 結果爲 "+result);
	}
	
	public void afterThrowing(JoinPoint joinPoint,Exception ex){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("異常通知方法 "+methodName+" 發生的異常爲 "+ex);
	}
	
	public Object aroundMethod(ProceedingJoinPoint pjp){
		
		Object result = null;
		String methodName = pjp.getSignature().getName();
		
		try {
			//前置通知
			System.out.println("環繞通知中的前置通知方法 "+methodName+" 開始於參數 "+Arrays.asList(pjp.getArgs()));
			//執行目標
			result = pjp.proceed();
			//返回通知
			System.out.println("環繞通知中的返回通知方法 "+methodName+" 結果爲 "+result);
		} catch (Throwable e) {
			//異常通知
			System.out.println("環繞通知中的異常通知方法 "+methodName+" 結果爲 "+e);
			throw new RuntimeException(e);
		}
		//後置通知
		System.out.println("環繞通知中的後置通知方法 "+methodName+" 結束了 ");
		
		return result;
	}
}
4、applicationContext-xml.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 配置bean -->
	<bean id="arithmeticCalculator" class="com.at.aop.xml.ArithmeticCalculatorImpl"></bean>
	
	<!-- 配置切面的bean -->
	<bean id="loggingAspect" class="com.at.aop.xml.LoggingAspect"></bean>
	
	<!-- 配置 AOP  -->
	<aop:config>
		<!-- 配置切點表達式 -->
		<aop:pointcut expression="execution(* com.at.aop.xml.ArithmeticCalculator.*(int, int))" id="pointcut"/>
		<!-- 配置切面及通知 -->
		<aop:aspect ref="loggingAspect">
			<!-- 前置通知 -->
		 	<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
			<!-- 後置通知 -->
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
			<!-- 返回通知 -->
			<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
			<!-- 異常通知 -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
			<!-- 環繞通知 -->
			<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
</beans>
5、測試函數

package com.at.aop.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAspect {

	public static void main(String[] args) {
		
		//1、創建spring的ioc容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
		System.out.println("=========");
		//2、從ioc容器中獲取bean的事例
		ArithmeticCalculator ac = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
		
		//3、使用bean
		int result = ac.add(3, 6);
		System.out.println("result "+result);
		
		int result2 = ac.div(12, 2);
		System.out.println("result "+result2);
	}
}
6、測試結果

信息: Loading XML bean definitions from class path resource [applicationContext-xml.xml]
=========
前置通知方法 add 開始 [3, 6]
環繞通知中的前置通知方法 add 開始於參數 [3, 6]
環繞通知中的返回通知方法 add 結果爲 9
環繞通知中的後置通知方法 add 結束了 
返回通知方法 add 結果爲 9
後置通知方法 add 結束 
result 9
前置通知方法 div 開始 [12, 2]
環繞通知中的前置通知方法 div 開始於參數 [12, 2]
環繞通知中的返回通知方法 div 結果爲 6
環繞通知中的後置通知方法 div 結束了 
返回通知方法 div 結果爲 6
後置通知方法 div 結束 
result 6

三、小結

正常情況下, 基於註解的聲明要優先於基於 XML 的聲明。通過 AspectJ 註解,切面可以與 AspectJ 兼容,而基於 XML 的配置則是 Spring 專有的。由於 AspectJ得到越來越多的 AOP 框架支持,所以以註解風格編寫的切面將會有更多重用的機會。

By luoyepiaoxue2014

微博地址: http://weibo.com/luoyepiaoxue2014 點擊打開鏈接



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