Spring AOP 之 环绕通知
在所有的通知类型中,环绕通知最为强大。因为它能完全控制方法的执行过程,所以可以把前一篇文章中所有通知动作都合并到一个单独的通知里。甚至可以控制是否以及何时继续执行原始方法。
在Spring AOP中,环绕通知必须实现MethodInterceptor接口。这个接口是AOP联盟为了保持不同AOP框架之间的兼容性而定义的。当编写环绕通知时,必须记住——如果要继续执行原始方法,那么就必须调用methodInvocation.proceed()。如果忘记执行这一步,那么原始方法将不会被调用。下面的环绕通知合并了前面所创建的前置、后置和异常通知。
package org.mahz.easyaop.calculator.aop; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LoggingAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { String methodName = invocation.getMethod().getName(); printTitle(methodName); System.out.println("The method " + methodName + "()begin with " + Arrays.toString(invocation.getArguments())); try { Object returnValue = invocation.proceed(); System.out.println("The Method " + methodName + "() ends with " + returnValue); return returnValue; } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); throw e; } } private void printTitle(String methodName) { System.out.println("=========================================="); System.out.println("============ Test " + methodName + " Method ============="); System.out.println("=========================================="); } }
环绕通知类型非常强大、非常灵活,你甚至可以改变原始的方法参数值以及最终的返回结果。但是因为很容易忘记执行原始的方法,所以在使用这种通知时必须非常小心。
因为这个通知结合了前面所有通知,所以只需要为代理指定这一个通知即可。
<bean id="arithmeticCalculator" class="org.mahz.easyaop.calculator.impl.ArithmeticCalculatorImpl" /> <bean id="unitCalculator" class="org.mahz.easyaop.calculator.impl.UnitCalculatorImpl" /> <!-- <bean id="logginBeforeAdvice" class="org.mahz.easyaop.calculator.aop.LoggingBeforeAdvice" /> <bean id="logginAfterAdvice" class="org.mahz.easyaop.calculator.aop.LoggingAfterAdvice" /> <bean id="logginThrowsAdvice" class="org.mahz.easyaop.calculator.aop.LoggingThrowsAdvice" /> --> <bean id="logginAroundAdvice" class="org.mahz.easyaop.calculator.aop.LoggingAroundAdvice" /> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Calculator</value> </list> </property> <property name="interceptorNames"> <list> <!-- <value>logginBeforeAdvice</value> <value>logginAfterAdvice</value> <value>logginThrowsAdvice</value> --> <value>logginAroundAdvice</value> </list> </property> </bean>
执行结果:
========================================== ============ Test add Method ============= ========================================== The method add()begin with [4.0, 0.0] 4.0 + 0.0 = 4.0 The Method add() ends with 4.0 ========================================== ============ Test sub Method ============= ========================================== The method sub()begin with [4.0, 0.0] 4.0 - 0.0 = 4.0 The Method sub() ends with 4.0 ========================================== ============ Test mul Method ============= ========================================== The method mul()begin with [4.0, 0.0] 4.0 * 0.0 = 0.0 The Method mul() ends with 0.0 ========================================== ============ Test div Method ============= ========================================== The method div()begin with [4.0, 0.0] Division by zero
相关推荐
Spring AOP配置的实例,最基本的Spring AOP配置
springAOP配置实现动态代理,有利于熟悉动态代理原理,深入了解spring。
Spring AOP配置事务方法,描述了spring的事务配置,方便开发应用和数据库的连接管理。
SpringAop xml方式配置通知
SpringAop面向切面编程
使用Spring配置文件实现AOP
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
spring aop做的日志管理,网上看的,没测试过。
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
1、编写切面类,包含权限审核方法和日志记录方法,这两个方法将来会织入到...5、编写案例,运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。请掌握这五种通知的特点,及应用场景
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
Spring AOP与事物管理.pdf 很好很全面
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
里面包含了多种Spring AOP配置,十分详细。
利用SPring AOP配置切面的一个例子.
springAop与spring定时器
springIOC与AOP配置