Spring-XML配置AOP-案例

来源:互联网 发布:2016淘宝卖什么赚钱 编辑:程序博客网 时间:2024/04/20 17:33

需求:分别在AtithmeticCalculator.java实现了该接口的类的加减乘除的方法执行时打印方法执行情况,并在方法执行前判断所有参数是否都是int,用aop实现。

接口实现类:AtithmeticCalculatorImpl.java,和上一个案例一样:http://blog.csdn.net/cuigaochong/article/details/49592861

只是在类中去掉所有的注解:通过xml配置的方式完成

配置文件: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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置bean --><bean id="atithmeticCalculatorImpl" class="com.cgc.spring.aop.xml.impl.AtithmeticCalculatorImpl"></bean><!-- 配置切面bean --><bean id="loggingAspect" class="com.cgc.spring.aop.xml.impl.LoggingAspect"></bean><bean id="vlidationAspect" class="com.cgc.spring.aop.xml.impl.VlidationAspect"></bean><!-- 配置AOP --><aop:config><!-- 配置切点表达式 --><aop:pointcut expression="execution(* com.cgc.spring.aop.xml.impl.AtithmeticCalculatorImpl.*(int, int))" id="pointcut"/><!-- 配置切面及通知 --><aop:aspect ref="loggingAspect" order="2"><aop:before method="beforeMethod" pointcut-ref="pointcut"/><aop:after method="afterMethod"  pointcut-ref="pointcut"/><aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/><aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" /></aop:aspect><aop:aspect ref="vlidationAspect" order="1"><aop:before method="validateArrgs" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>

2 0
原创粉丝点击