返回通知&异常通知&环绕通知
【返回通知】
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” 百 ... 
随机推荐
- EasyUI Form表单提交
			转自:https://www.cnblogs.com/net5x/articles/4576926.html Form(表单) 使用$.fn.form.defaults重写默认值对象 form提供了各 ... 
- sql2000数据库置疑造成的原因以及如何解决置疑
			造成数据库置疑一般有以下几点: 1)电脑非法关机或者意外停电: 2)磁盘有坏道或者损坏: 3)数据库感染病毒,日志文件损坏: 4)非正常情况下移动数据库文件 5)系统,硬盘,经常强制性关机(如断电)类 ... 
- lodop 打印
			使用Lodop打印: 一.在官网下载http://www.lodop.net/download.html 若是安装还是提示未安装,就按转这个 二.准备两个js 三.需要在页面最上面加入 <htm ... 
- iOS 关于文件操作 NSFileManager
			创建文件夹 + (BOOL)creatDir:(NSString *)path { if (path.length==0) { return NO; } NSFileManager *fileMana ... 
- 贪心 CodeForces 137B Permutation
			题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ... 
- Android 性能优化(3)性能工具之「调试 GPU 过度绘制」Debug GPU Overdraw Walkthrough-查看哪些view过度绘制了
			Debug GPU Overdraw Walkthrough 1.In this document Prerequisites Visualizing Overdraw You should also ... 
- 282 Expression Add Operators 给表达式添加运算符
			给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ... 
- js解析地址栏参数
			/** * 获取地址栏中url后面拼接的参数 * eg: * 浏览器地址栏中的地址:http://1.1.1.1/test.html?owner=2db08226-e2fa-426c-91a1-66e ... 
- php域名授权只需要一个函数
			<?php function allow_doamin(){ $is_allow=false; $url=trim($_SERVER['SERVER_NAME']); $arr_allow_do ... 
- OKHTTP 简单分析
			内部使用了OKIO库, 此库中Source表示输入流(相当于InputStream),Sink表示输出流(相当于OutputStream) 特点: ·既支持同步请求,也支持异步请求,同步请求会阻塞当前 ... 
