【返回通知】

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;
}

运行结果:

假如发生异常:

运行结果:

返回通知&异常通知&环绕通知的更多相关文章

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

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

  2. spring学习 十 schema-based 异常通知,和环绕通知

    一 schema-based异常通知 第一步:创建通知类 :新建一个类实现 throwsAdvice 接口,throwsAdvice接口只是标记接口里面并没有任何方法,必须自己写方法,且必须叫 aft ...

  3. 通知类型 重点: 环绕通知 (XML配置)

    前置通知:在切入点方法执行之前执行 <aop:before method="" pointcut-ref="" ></aop:before&g ...

  4. 环绕通知(xml)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  5. 日志之环绕通知(AOP)

    环绕通知:一个完整的try...catch...finally结构 编写环绕通知方法,环绕通知需要携带ProceedingJoinPoint 这个类型的参数,ProceedingJoinPoint类型 ...

  6. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...

  8. Spring AOP--返回通知,异常通知和环绕通知

    在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...

  9. 转:环绕通知返回值 object 类型

    遇到 AOP 环绕通知报错  “return value from advice does not match primitive return type for: public boolean” 百 ...

随机推荐

  1. mysql通用分页存储过程遇到的问题(转载)

    mysql通用分页存储过程遇到的问题(转载) http://www.cnblogs.com/daoxuebao/archive/2015/02/09/4281980.html

  2. map的遍历方式(使用Junit测试)

    package cn.sdut.lah; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...

  3. bzoj1216 操作系统(优先队列模拟)

    1216: [HNOI2003]操作系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1172  Solved: 649[Submit][Statu ...

  4. 清北考前刷题day4下午好

    /* 辗转相除,每次计算多出现了几个数. */ #include<iostream> #include<cstdio> #include<cstring> #inc ...

  5. sshd服务器搭建管理和防止暴力破解

    1.1 Linux服务前期环境准备,搭建一个RHEL7环境 1.2 sshd服务安装-ssh命令使用方法 1.3 sshd服务配置和管理 1.4 防止SSHD服务暴力破解的几种方式 1.1 Linux ...

  6. pyDes 实现 Python 版的 DES 对称加密/解密--转

    https://my.oschina.net/leejun2005/blog/586451 手头有个 Java 版的 DES 加密/解密程序,最近想着将其 Python 重构下,方便后续脚本解析,捣鼓 ...

  7. Props、State、Refs 与表单处理

    我们也了解到 React Component 事实上可以视为显示 UI 的一个状态机(state machine),而这个状态机根据不同的 state(透过 setState() 修改)和 props ...

  8. Android基础TOP6_2:Gallery +Image完成画廊

    Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  9. table的数据行tr上下移动

    昨天帮别人解决一个前端页面表格里的数据行上下移动的前端效果,直奔google找了几个demo,发现demo是实现了效果,但是代码很多,最后还是决定自己用jquery写个吧, 首先将前端效果分析出编程逻 ...

  10. UEditer的使用

    1.首先到官网下载http://ueditor.baidu.com/website/download.html#ueditor 2.然后把解压后的文件复制到项目中(放在UEditer中),如图 3.在 ...