【返回通知】

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     }

运行结果:

假如发生异常:

运行结果:

转载于:https://www.cnblogs.com/zhzcode/p/9670720.html

返回通知异常通知环绕通知相关推荐

  1. Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

    AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...

  2. (Spring笔记)AspectJ环绕通知——@Around切面开发

    目录 一.前言 二.环绕通知切面开发         (1)环绕通知业务接口         (2)环绕通知业务接口实现         (3)环绕通知切面类         (4)applicati ...

  3. Spring环绕通知

    Spring环绕通知 /*** @throws Throwable * @Around 环绕通知是spring中最强大的通知方法,其本身就是一个动态代理* * 通知方法* try{* @Before* ...

  4. Spring Boot AOP面向切面编程使用(定义切入点、前置通知、后置通知、返回通知、异常通知、环绕通知)

    1 AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发 ...

  5. spring之aop(前置通知,后置通知,环绕通知,过滤通知,异常通知)

    1.AOP中关键性概念  连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻辑 通知(Advice ...

  6. spring之AOP(面向切面编程)和五大通知(前置通知、后置通知、异常通知、环绕通知、过滤通知)

    一.aop的介绍 1.AOP中关键性概念 : 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出. 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻 ...

  7. 配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知

    环绕通知 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  8. Spring3.0中的前置通知、后置通知、环绕通知、异常通知

    观众类Audience~~ [java] view plain copy package com.jCuckoo.demo; import org.aspectj.lang.ProceedingJoi ...

  9. Spring的环绕通知

    首先加入jar包: com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org.aopalliance-1.0.0 .jar com.s ...

最新文章

  1. 二叉树的建立和遍历程序代码(Java,C)
  2. hadoop 安全模式
  3. 黑马程序员 oc对象的方法成员变量
  4. 用Session和唯一索引字段实现通用Web分页功能
  5. 重定向和转发之间的区别
  6. [原]windbg调试系列——崩溃在ComFriendlyWaitMtaThreadProc
  7. Lock VS Monitor
  8. android 的属性动画
  9. LeetCode 76. 最小覆盖子串(滑动窗口)
  10. 马云曾卖鲜花,柳传志卖冰箱!摆摊吧,程序员!
  11. python compile函数_python 内置函数 compile()
  12. 学Java看什么视频好呢?视频经验大总结
  13. 《云计算核心技术剖析》读书笔记之一
  14. 房地产“产权分割制”是什么大杀器?
  15. Selenium实现QQ空间说说自动点赞
  16. Linux下的编曲软件,在Linux下可用Wine运行编曲软件FL Studio,也可用LMMS替代
  17. 浅谈Suffix Automaton(后缀自动机)
  18. leetcode 717. 1比特与2比特字符(python)
  19. Python selenium —— 一定要会用selenium的等待,三种等待方式解读
  20. python实验报告实验目的_20192217 实验一《Python程序设计》实验报告

热门文章

  1. 非常不错的Coding-iOS开源项目
  2. 打开.net web项目,出现Unable to get the project ile from the web server错误
  3. Git tag常用命令分享
  4. 读书笔记 effctive c++ Item 20 优先使用按const-引用传递(by-reference-to-const)而不是按值传递(by value)...
  5. ant+testng 搭建
  6. CI 里去掉index.php 并 识别css js 文件 技巧
  7. udp helper 的使用
  8. 数据可视化图表的类型和特点
  9. 计算机主板及其硬件图,[计算机硬件及网络]intel G43主板电路图.pdf
  10. java亚马逊模拟登录_java – 亚马逊MWS入门