5Spring AOP的几种通知(xml)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5Spring AOP的几种通知(xml)相关的知识,希望对你有一定的参考价值。
1、前置、后置、返回、异常和环绕
1 public interface Calculator { 2 3 int add(int a, int b); 4 5 int sub(int a, int b); 6 } 7 8 public class CalculatorImpl implements Calculator { 9 10 @Override 11 public int add(int a, int b) { 12 return a + b; 13 } 14 15 @Override 16 public int sub(int a, int b) { 17 try { 18 int i = 1 / 0; 19 } catch (Exception e) { 20 } 21 return a - b; 22 } 23 24 }
1 public void before(JoinPoint joinPoint) { 2 System.out.println("前置通知。。。"); 3 String joinPointName = joinPoint.getSignature().getName(); 4 System.out.println("开始执行 " + joinPointName + " 方法。。。参数: " + Arrays.toString(joinPoint.getArgs())); 5 } 6 7 public void after(JoinPoint joinPoint) { 8 System.out.println("后置通知。。。"); 9 String joinPointName = joinPoint.getSignature().getName(); 10 System.out.println(joinPointName + " 方法执行完毕。。。"); 11 } 12 13 public void afterReturning(JoinPoint joinPoint, Object result) { 14 System.out.println("返回通知。。。"); 15 String joinPointName = joinPoint.getSignature().getName(); 16 System.out.println(joinPointName + " 方法执行完毕。。。\n返回值是: " + result); 17 } 18 19 public void afterThrowing(JoinPoint joinPoint, Exception e) { 20 System.out.println("异常通知。。。"); 21 String joinPointName = joinPoint.getSignature().getName(); 22 System.out.println(joinPointName + " 方法抛出异常。。。\n异常信息: " + e.getMessage()); 23 } 24 25 public Object around(ProceedingJoinPoint pJoinPoint) { 26 System.out.println("环绕通知。。。"); 27 String joinPointName = pJoinPoint.getSignature().getName(); 28 Object[] args = pJoinPoint.getArgs(); 29 System.out.println("开始执行 " + joinPointName + " 方法。。。参数: " + Arrays.toString(args)); 30 Object result = null; 31 try { 32 result = pJoinPoint.proceed(); 33 // result = pJoinPoint.proceed(args); 34 } catch (Throwable e) { 35 e.printStackTrace(); 36 } 37 38 System.out.println(joinPointName + " 方法执行完毕。。。"); 39 40 return result; 41 }
配置:
1 <bean id="calculator" class="demo.CalculatorImpl" /> 2 3 <bean id="calculatorLoggingAspect" class="demo.CalculatorLoggingAspect" /> 4 5 <aop:config> 6 <aop:pointcut id="pointCut" expression="execution(* demo.*.add(..))" /> 7 <aop:aspect id="logging" ref="calculatorLoggingAspect"> 8 <!-- <aop:before method="before" pointcut-ref="pointCut" /> --> 9 <!-- <aop:after method="after" pointcut-ref="pointCut" /> --> 10 <!-- <aop:after-returning method="afterReturning" returning="result" pointcut-ref="pointCut"/> --> 11 <!-- <aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="pointCut" /> --> 12 <aop:around method="around" pointcut-ref="pointCut"/> 13 </aop:aspect> 14 </aop:config>
2、引入通知
1 public interface MinCalculator { 2 3 int min(int a, int b); 4 5 } 6 7 public class MinCalculatorImpl implements MinCalculator{ 8 9 @Override 10 public int min(int a, int b) { 11 return a < b ? a : b; 12 } 13 14 }
配置:
1 <aop:declare-parents types-matching="demo.*" 2 implement-interface="demo.MinCalculator" default-impl="demo.MinCalculatorImpl" />
3、测试
1 Object obj = ctx.getBean("calculator"); 2 Calculator cal = (Calculator) obj; 3 int r = cal.add(1, 1); 4 System.out.println(r); 5 6 MinCalculator min = (MinCalculator)obj; 7 r = min.min(1, 3); 8 System.out.println(r);
以上是关于5Spring AOP的几种通知(xml)的主要内容,如果未能解决你的问题,请参考以下文章