Spring之使用AOP缘由(4-SpringAOP实现-XML配置方式)

 

除了使用AspectJ注解声明切面,Spring 也支持在 Bean 配置文件中声明切面。这种声明是通过 aop schema 中的 XML 元素完成的。正常情况下,基于注解的声明要优先于基于 XML 的声明。通过 AspectJ 注解,切面可以与 AspectJ 兼容,而基于 XML 的配置则是 Spring 专有的。由于 AspectJ 得到越来越多的 AOP 框架支持,所以以注解风格编写的切面将会有更多重用的机会。

使用SpringAOP实现

 

1. AOP依赖包:

    1.1 spring包:

          核心包:

          spring-framework-4.0.0.RELEASE-dist:

          spring-beans-4.0.0.RELEASE.jar

          spring-context-4.0.0.RELEASE.jar

          spring-core-4.0.0.RELEASE.jar

          spring-expression-4.0.0.RELEASE.jar

   

          适配AspectJ包:

          spring-aop-4.0.0.RELEASE.jar

          spring-aspects-4.0.0.RELEASE.jar

 

    1.2 commons包:

          commons-logging-1.1.1.jar

 

    1.3 aspectj包:

          com.springsource.org.aopalliance-1.0.0.jar

          com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

 

2. SpringAOP配置步骤:

    2.1 加入AOP依赖包;

    2.2 在配置文件中加入aop的命名空间;

    2.3 基于XML的方式;

 

    2.3.1 声明切面

             当使用 XML 声明切面时,需要在 <beans> 根元素中导入 aop Schema。在 Bean 配置文件中,所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部。对于每个切面而言,都要创建一个 <aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例。切面 Bean 必须有一个标示符,供 <aop:aspect> 元素引用。

    2.3.2 声明切入点

             切入点使用 <aop:pointcut> 元素声明。切入点必须定义在 <aop:aspect> 元素下,或者直接定义在 <aop:config> 元素下。定义在 <aop:aspect> 元素下: 只对当前切面有效。定义在 <aop:config> 元素下::对所有切面都有效。基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点。

    2.3.3 声明通知

             在 aop Schema 中,每种通知类型都对应一个特定的 XML 元素。通知元素需要使用 <pointcut-ref> 来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式。method 属性指定切面类中通知方法的名称。

    2.3.4 声明引入

             可以利用 <aop:declare-parents> 元素在切面内部声明引入。

 

3. 添加切面模型类并配置

 

package xyz.huning.spring4.aop.calculator.aopimpl.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class CalculatorValidateAspect {

	public void validate(JoinPoint joinPoint)
	{
		System.out.println("validate parameter: " + Arrays.toString(joinPoint.getArgs()));
	}

}

 

package xyz.huning.spring4.aop.calculator.aopimpl.xml;

import org.aspectj.lang.JoinPoint;

public class CalculatorLogAspect {

	public void before(JoinPoint joinPoint)
	{
		System.out.println("before: " + "[x=" + joinPoint.getArgs()[0] + ", y=" + joinPoint.getArgs()[1] + "]");
	}

	public void after(JoinPoint joinPoint)
	{
		System.out.println("after: " + "[x=" + joinPoint.getArgs()[0] + ", y=" + joinPoint.getArgs()[1] + "]");
	}
	
}

 

4. 添加配置文件

 

<?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="pureCalculator" class="xyz.huning.spring4.aop.calculator.PureCalculator"></bean>

    <!--配置切面bean-->
	<bean id="calculatorValidateAspect" class="xyz.huning.spring4.aop.calculator.aopimpl.xml.CalculatorValidateAspect"></bean>
	<bean id="calculatorLogAspect" class="xyz.huning.spring4.aop.calculator.aopimpl.xml.CalculatorLogAspect"></bean>
	
	<!--配置AOP-->
	<aop:config>
		<!--配置切点表达式-->
		<aop:pointcut expression="execution(public double xyz.huning.spring4.aop.calculator.PureCalculator.*(..))" id="pointcut"/>
		
		<!--配置切面及通知-->
		<aop:aspect ref="calculatorValidateAspect" order="1">
			<aop:before method="validate" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		<aop:aspect ref="calculatorLogAspect" order="2">
			<aop:before method="before" pointcut-ref="pointcut"/>
			<aop:after method="after" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
	
</beans>

 

5. 添加测试类

 

package xyz.huning.spring4.aop.calculator.aopimpl.xml;

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

import xyz.huning.spring4.aop.calculator.ICalculator;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("calculator-aop-xml.xml");
		
		ICalculator calc = ctx.getBean("pureCalculator", ICalculator.class);
		calc.add(10, 20);
		calc.sub(30, 10);
		calc.mul(10, 10);
		calc.div(20, 10);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 

6. 执行结果

 



 

 

 

猜你喜欢

转载自ihuning.iteye.com/blog/2226703