返回通知&异常通知&环绕通知
【返回通知】
LoggingAspect.java:
@Aspect
@Component
public class LoggingAspect {
/*
* 在方法正常执行后执行的通知叫返回通知
* 返回通知是可以访问到方法的返回值的
*/
@AfterReturning(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))",
returning="result")
public void afterReturning(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
} }
Main.java:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("ArithmeticCalculator");
System.out.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(1, 2);
System.out.println("result: " + result);
result = arithmeticCalculator.div(1000, 10);
System.out.println("result " + result);
}
}
运行结果:

【异常通知】
1.只在连接点抛出异常时才执行异常通知。
2.将Throwing属性添加到@AfterThrowing注解中,也可以访问连接点抛出的异常。Throwable是所有错误和异常类的超类。所以在异常通知方法可以捕获到任何错误和异常。
3.如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他类型的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行。
LoggingAspect.java:
/*
* 在目标方法出现异常时,会执行代码。
* 可以访问到异常对象;且可以指定在出现特定异常时在执行通知
*/
@AfterThrowing(value="execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))",
throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " coours exception : " + ex);
}
Main.java:
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("ArithmeticCalculator");
System.out.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(1, 2);
System.out.println("result: " + result);
result = arithmeticCalculator.div(1000, 0);
System.out.println("result " + result);
}
}
运行结果:

注:1000/0 发生异常。
【环绕通知】
LoggingAspect.java:
/*
* 环绕通知需要携带ProceedingJoinPoint 类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint这个类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值即为目标方法的返回值
*/
@Around("execution(public int com.hk.spring.aop.notice.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null;
String methodName = pjd.getSignature().getName(); //执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + "begins with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();
//后置通知
System.out.println("The method " + methodName + "ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method occurs exception:" + e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
运行结果:

假如发生异常:
运行结果:

返回通知&异常通知&环绕通知的更多相关文章
- Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)
AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...
- spring学习 十 schema-based 异常通知,和环绕通知
一 schema-based异常通知 第一步:创建通知类 :新建一个类实现 throwsAdvice 接口,throwsAdvice接口只是标记接口里面并没有任何方法,必须自己写方法,且必须叫 aft ...
- 通知类型 重点: 环绕通知 (XML配置)
前置通知:在切入点方法执行之前执行 <aop:before method="" pointcut-ref="" ></aop:before&g ...
- 环绕通知(xml)
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- 日志之环绕通知(AOP)
环绕通知:一个完整的try...catch...finally结构 编写环绕通知方法,环绕通知需要携带ProceedingJoinPoint 这个类型的参数,ProceedingJoinPoint类型 ...
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 19Spring返回通知&异常通知&环绕通知
在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...
- Spring AOP--返回通知,异常通知和环绕通知
在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...
- 转:环绕通知返回值 object 类型
遇到 AOP 环绕通知报错 “return value from advice does not match primitive return type for: public boolean” 百 ...
随机推荐
- 0629-TP整理四(create(),success(),error(),U())
create()-前提:表单中name的值要与数据库中的字段一一匹配 可直接获取表单数据进行操作: 作用:将数据库中没有的字段在数组中去除. PHP中添加的语法如下: success()和error( ...
- 【React Native】React Native项目设计与知识点分享
闲暇之余,写了一个React Native的demo,可以作为大家的入门学习参考. GitHub:https://github.com/xujianfu/ElmApp.git GitHub:https ...
- 读懂mysql慢查询日志
我们来看一下如何去读懂这些慢查询日志.在跟踪慢查询日志之前,首先你得保证最少发生过一次慢查询.如果你没有可以自己制造一个:root@server# mysql -e 'SELECT SLEEP(8); ...
- Eclipse里的Java EE视图在哪里?MyEclipse里的Java EE视图在哪里?MyEclipse里的MyEclipse Java Enterprise视图在哪里?(图文详解)
为什么要写这篇博客呢? 是因为,最近接触一个web项目. 然后呢,Eclipse里的Java EE视图的位置与MyEclipse里不太一样.为了自己梳理日后查找,也是为了新手少走弯路. Eclipse ...
- js截取字符串 区分中英文
方法如下: //在一个字符串中截取前面部分文字,汉字.全角符号按2个占位,数字英文.半角按一个占位,未显示完的最后加入“……”. //适合多行显示. function suolve(str, sub_ ...
- 高性能队列disruptor为什么这么快?
背景 Disruptor是LMAX开发的一个高性能队列,研发的初衷是解决内存队列的延迟问题(在性能测试中发现竟然与I/O操作处于同样的数量级).基于Disruptor开发的系统单线程能支撑每秒600万 ...
- Echarts修改legend样式
legend: { icon: 'rect', itemWidth: 20, itemHeight: 10, itemGap: 10}
- 如何删除sublime目录
左侧栏的sublime目录一直删不掉,删除列直接变成了灰色. 今天才发现应该选择文件夹右击选择工程——从工程中删除文件夹. 这个设计真的很醉,删除这么常用的键还放进了第二层……
- Hue - Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or
解决下面两点异常 >> 1. Hue页面 点击DB 查询时弹出: Error loading MySQLdb module: libmysqlclient.so.20: cannot op ...
- Statement和PreparedStatement深入学习总结
最近在看java安全编码方面的书籍,在看到SQL注入漏洞的问题时,引发了我对Statement和PreparedStatement深入总结的欲望,废话少说,下面咱们就正式开始. 当初始的SQL查询被修 ...