返回通知&异常通知&环绕通知
【返回通知】
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” 百 ...
随机推荐
- E20170602-ts
questionnaire n. 调查问卷; 调查表; アンケート不是英语 collection n. 征收; 收集,采集; 收藏品; 募捐; association n. 联想; 协会, ...
- 第四代增强 源代码增强(ABAP Source Code Enhancements)
显式代码增强的创建 se38打开你要增强的程序 进入编辑状态 在菜单栏选择: Edit->Enhancement Opreations->Create option. 此时弹出Create ...
- 为什么前后端分离不利于seo
搜索引擎的基础爬虫的原理就是抓取你的url,然后获取你的html源代码并解析. 而你的页面通常用了vue等js的数据绑定机制来展示页面数据,爬虫获取到的html是你的模型页面而不是最终数据的渲染页面, ...
- $Hdu1381\ Crazy\ Search$
前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp <& ...
- JavaScript--userAgent
userAgent 返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串) 语法 navigator.userAgent 几种浏览的user_agent.,像360的兼容模式用的是IE.极速模 ...
- 410 Split Array Largest Sum 分割数组的最大值
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件: 1 ≤ n ≤ 1000 ...
- hibernate关联关系查询
关联关系 一对一 A中包含B的对象,B中包含A的对象 一对多 A中包含B的集合,B中包含A的对象 多对多 A中包含B的集合,B中包含A的集合 1,一对多配置 一名老师可以对应多名学生 2,模型类 老师 ...
- P2P 网络核心技术:Gossip 协议
背景 Gossip protocol 也叫 Epidemic Protocol (流行病协议),实际上它还有很多别名,比如:“流言算法”.“疫情传播算法”等. 这个协议的作用就像其名字表示的意思一样, ...
- [ Luogu 3935 ] Calculating
\(\\\) \(Description\) 若\(x\)分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n}\),令\(f(x)=(k_1+1)(k_2+1) ...
- java实现麦克风自动录音
最近在研究语音识别,使用百度的sdk.发现只有识别的部分,而我需要保存音频文件,并且实现当有声音传入时自动生成音频文件. 先上代码: public class EngineeCore { String ...