返回通知&异常通知&环绕通知

【返回通知】

LoggingAspect.java:

 1 @Aspect
 2 @Component
 3 public class LoggingAspect {
 4     /*
 5      * 在方法正常执行后执行的通知叫返回通知
 6      * 返回通知是可以访问到方法的返回值的
 7      */
 8     @AfterReturning(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))",
 9                     returning="result")
10     public void afterReturning(JoinPoint joinPoint,Object result){
11         String methodName = joinPoint.getSignature().getName();
12         System.out.println("The method " + methodName + " ends with " + result);
13     }
14  
15 }

Main.java:

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 5         ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("ArithmeticCalculator");
 6         
 7         System.out.println(arithmeticCalculator.getClass().getName());
 8         
 9         int result = arithmeticCalculator.add(1, 2);
10         System.out.println("result: " + result);
11         
12         result = arithmeticCalculator.div(1000, 10);
13         System.out.println("result " + result);
14 
15     }
16 
17 }

运行结果:

【异常通知】

1.只在连接点抛出异常时才执行异常通知。

2.将Throwing属性添加到@AfterThrowing注解中,也可以访问连接点抛出的异常。Throwable是所有错误和异常类的超类。所以在异常通知方法可以捕获到任何错误和异常。

3.如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他类型的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行。

LoggingAspect.java:

 1 /*
 2      * 在目标方法出现异常时,会执行代码。
 3      * 可以访问到异常对象;且可以指定在出现特定异常时在执行通知
 4      */
 5     @AfterThrowing(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))",
 6                    throwing="ex")
 7     public void afterThrowing(JoinPoint joinPoint,Exception ex){
 8         String methodName = joinPoint.getSignature().getName();
 9         System.out.println("The method " + methodName + " coours exception : " + ex);
10     }

Main.java:

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 5         ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("ArithmeticCalculator");
 6         
 7         System.out.println(arithmeticCalculator.getClass().getName());
 8         
 9         int result = arithmeticCalculator.add(1, 2);
10         System.out.println("result: " + result);
11         
12         result = arithmeticCalculator.div(1000, 0);
13         System.out.println("result " + result);
14 
15     }
16 
17 }

运行结果:

注:1000/0 发生异常。

【环绕通知】

LoggingAspect.java:

 1     /*
 2      * 环绕通知需要携带ProceedingJoinPoint 类型的参数
 3      * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint这个类型的参数可以决定是否执行目标方法
 4      * 且环绕通知必须有返回值,返回值即为目标方法的返回值
 5      */
 6     @Around("execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))")
 7     public Object aroundMethod(ProceedingJoinPoint pjd){
 8         
 9         Object result = null;
10         String methodName = pjd.getSignature().getName();
11         
12         //执行目标方法
13         try {
14             //前置通知
15             System.out.println("The method " + methodName + "begins with " + Arrays.asList(pjd.getArgs()));
16             result = pjd.proceed();
17             //后置通知
18             System.out.println("The method " + methodName + "ends with " + result);
19         } catch (Throwable e) {
20             //异常通知
21             System.out.println("The method occurs exception:" + e);
22         }
23         //后置通知
24         System.out.println("The method " + methodName + " ends");
25         return result;
26     }

运行结果:

假如发生异常:

运行结果:

猜你喜欢

转载自www.cnblogs.com/zhzcode/p/9670720.html