常用增强处理类型

增强处理类型                                                        特点
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. 使用fastDFS上传和下载图片文件

    package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...

  2. 使用ajax对用户注册时,用户名进行检验

    package cn.hopetesting.com.servlet;import com.fasterxml.jackson.databind.ObjectMapper;import javax.p ...

  3. synchronized底层浅析(一)

    之前说过hashMap,我们知道hashMap是一种非线程安全的集合,主要原因是它在多线程的情况下,插入.删除.扩容的时候容易导致数据丢失或者链表环 那我们也知道ConcurrentHashMap.h ...

  4. VUE3 之 template 语法

    1. 概述 老话说的好:干一行,爱一行,踏实工作才是真正快乐的源泉. 言归正传,今天继续聊 VUE3 的话题,今天聊聊 template 语法. 闲话不多说,直接上代码. 2. template 语法 ...

  5. RocketMQ源码详解 | Broker篇 · 其五:高可用之主从架构

    概述 对于一个消息中间件来讲,高可用功能是极其重要的,RocketMQ 当然也具有其对应的高可用方案. 在 RocketMQ 中,有主从架构和 Dledger 两种高可用方案: 第一种通过主 Brok ...

  6. ciscn_2019_s_3

    拿到题目例行检查 64位程序开启了nx保护,将程序放入ida 看到没有system函数第一时间想到的就是泄露libc来做,后来才知道是我学识尚浅,需要用execve函数来做 进入main发现跳转到vu ...

  7. 测试开发实战[提测平台]17-Flask&Vue文件上传实现

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 先回顾下在此系列第8次分享给出的预期实现的产品原型和需求说明,如下图整体上和前两节实现很相似,只不过一般测试报告要写的内容可能比较多,就多 ...

  8. java 编程基础:【注解】 提取注解信息,利用自定义注解编写测试类,注解绑定事件

    提取注解信息 使用注解修饰了类.方法.成员变量等成员之后,这些注解不会自己生效,必须由开发者提供相应工具来提取并处理注解信息.   Java使用java.lang.annotation.Annotat ...

  9. java 常用类库:时间类LocalDate;LocalTime;LocalDateTime;Calendar 类;Date ;

    LocalDate类 LocalDate类代表不带时区的日期,列入2020-12-20.该类提供了静态的now()方法来获取当前的日期.这个类是线程安全的. LocalTime类 代表不带时区的时间, ...

  10. 雨课堂自动切换PPT代码

    浏览器运行js步骤 原仓库 Podium = {}; Podium.keydown = function(k) { var oEvent = document.createEvent('Keyboar ...