常用增强处理类型

增强处理类型                                                        特点
before 前置增强处理,在目标方法前织入增强处理
AfterReturning 后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理
AfterThrowing 异常增强处理,在目标方法抛出异常后织入增强处理
After 最终增强处理,不论方法是否抛出异常,都是会在目标方法最后织入增强处理
Around  环绕增强处理,在目标方法的前后都可以织入增强处理,同时可以控制目标方法的执行及返回值的再加工处理

环绕增强: 相当于拦截器,可以控制目标方法的执行,可以对目标方法的返回值再加工

  • Around Advice 的第一个参数必须是ProceedingJoinPoint类型,
  • 调用ProceedingJoinPoint类的proceed()方法来控制目标方法是否执行,
    • proceed()方法可以传入一个Object的数组,当方法执行时,数组内的值会当作参数传入
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
  • xml
<aop:aspect id="aroundExample" ref="aBean">

    <aop:around
pointcut-ref="businessService"
method="doBasicProfiling"/> ... </aop:aspect>
  • 控制目标方法是否执行
package x.y;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch; public class SimpleProfiler { public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
StopWatch clock = new StopWatch("Profiling for '" + name + "' and '" + age + "'");
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}
}
  • xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- this is the object that will be proxied by Spring's AOP infrastructure -->
<bean id="personService" class="x.y.service.DefaultPersonService"/> <!-- this is the actual advice itself -->
<bean id="profiler" class="x.y.SimpleProfiler"/> <aop:config>
<aop:aspect ref="profiler"> <aop:pointcut id="theExecutionOfSomePersonServiceMethod"
expression="execution(* x.y.service.PersonService.getPerson(String,int))
and args(name, age)"/> <aop:around pointcut-ref="theExecutionOfSomePersonServiceMethod"
method="profile"/> </aop:aspect>
</aop:config> </beans>

After(Finally) Advice 不管方法是否执行完成,After Advice都会执行,和Finally一样

<aop:aspect id="afterFinallyExample" ref="aBean">

    <aop:after
pointcut-ref="dataAccessOperation"
method="doReleaseLock"/> ... </aop:aspect>

After Throwing Advice 当执行目标方法抛出异常时,使用After Throwing Advice

  • 注意:是throwing Exception,try...catch是不起作用的
<aop:aspect id="afterThrowingExample" ref="aBean">

    <aop:after-throwing
pointcut-ref="dataAccessOperation"
method="doRecoveryActions"/> ... </aop:aspect>
  • 还可通过throwing属性指定异常信息的接收对象
<aop:aspect id="afterThrowingExample" ref="aBean">

    <aop:after-throwing
pointcut-ref="dataAccessOperation"
throwing="dataAccessEx"
method="doRecoveryActions"/> ... </aop:aspect>

参考资料:

https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#aop-schema-advice-after-throwing

spring-aop(二)学习笔记的更多相关文章

  1. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  2. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)

    绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...

  3. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  4. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  5. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  6. Spring中的AOP(学习笔记)

    是什么AOP及实现方式 AOP的基本概念 Schema-base AOP Spring AOP API AspectJ

  7. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring AOP(面向切面编程)

    面向切面编程(AOP)和面向对象编程(OOP)类似,也是一种编程模式.Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的. AOP ...

  10. Spring AOP 简单入门笔记 (转)

    分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...

随机推荐

  1. 安全刻不容缓「GitHub 热点速览 v.21.50」

    作者:HelloGitHub-小鱼干 本周最热的事件莫过于 Log4j 漏洞,攻击者仅需向目标输入一段代码,不需要用户执行任何多余操作即可触发该漏洞,使攻击者可以远程控制用户受害者服务器,90% 以上 ...

  2. Mysql配置文件 4c8g优化

    目录 一.说明 二.配置 一.说明 以下配置适合4核8G及以下的配置,会让性能稍微提高1/3左右. 测试语句 mysqlslap -uroot -p123456 --concurrency=100 - ...

  3. Linux服务器被黑 排查思路

    目录 一.为何会被入侵? 二.排查 入侵排查 检查是否还存在被登陆可能 计划任务 被修改的文件 筛选日志 日志恢复 找到异常进程-1 找到异常进程-2 找到异常进程-3 找到异常进程-4 三.总结 一 ...

  4. JavaScript中的NaN

    论装逼我只服NaN 首先这逼自己都不愿意等于自己 console.log(NaN == NaN); // false 这逼够嫌弃自己的 其次这逼本身的意思是非数字就是NaN 然鹅typeof NaN结 ...

  5. iphone4 与iphone5适配

    转:http://www.cnblogs.com/ygm900/archive/2013/05/24/3098015.html iphone4/4s 应用程序升级适配 iphone5 的方法有很多,而 ...

  6. python之异步编程

    一.异步编程概述 异步编程是一种并发编程的模式,其关注点是通过调度不同任务之间的执行和等待时间,通过减少处理器的闲置时间来达到减少整个程序的执行时间:异步编程跟同步编程模型最大的不同就是其任务的切换, ...

  7. [BUUCTF]REVERSE——刮开有奖

    刮开有奖 附件 步骤: 例行检查,无壳,32位程序 32位ida载入,shift+f12检索程序里的字符串,看到了一个base64加密的特征字符串,猜想这题用到了base64加密 从main函数开始看 ...

  8. selenium chrome一些配置问题

    chrome和chromedriver要对应,chrome版本查询:打开chrome,在地址栏输入:chrome://version/ 附: 所有chromedriver均可在下面链接中下载到:htt ...

  9. 如何把maven项目转为eclipse项目

    如何把maven项目转为eclipse项目,按照如下操作便可. 在cmd窗口, 载cmd窗口进入到maven项目所在目录下,输入如下命令: mvn eclipse:eclipse 这样便可.

  10. SpringBoot整合websocket简单示例

    依赖 <!-- springboot整合websocket --> <dependency> <groupId>org.springframework.boot&l ...