当前位置: 首页 > news >正文

Spring aop 五种通知类型

spring aop 的五种通知类型

  1. 前置通知 Before advice:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常
  2. 后置通知 After returning advice:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行
  3. 异常通知 After throwing advice:在连接点抛出异常后执行
  4. 最终通知 After (finally) advice:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容
  5. 环绕通知 Around advice:环绕通知围绕在连接点前后,能在方法调用前后自定义一些操作,还需要负责决定是继续处理 join point (调用 ProceedingJoinPointproceed 方法)还是中断执行

五大通知类型中,环绕通知功能最为强大,因为环绕通知,可以控制目标方法是否执行。
如果需要记录异常信息,使用异常通知。
其他通知,只能做记录工作,不能做处理,所以执行顺序其实对整个程序影响不大,没有必要太深究。

spring aop 通知类型使用

添加 Aspect 切面类

package com.example.demo.module.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Component
@Aspect
public class AdviceTest {// 配置织入点@Pointcut("execution(public * com.example.demo.module.aspect.Test.*(..))")public void test(){}@Before("test()")public void doBefore(JoinPoint joinPoint){System.out.println(joinPoint.getSignature().getName() + "执行前置通知---");}@AfterReturning("test()")public void doAfterSuccess(JoinPoint joinPoint){System.out.println(joinPoint.getSignature().getName() + "执行返回通知---");}@AfterThrowing("test()")public void doAfterError(JoinPoint joinPoint){System.out.println(joinPoint.getSignature().getName() + "执行异常通知---");}@Around(value = "test()", argNames = "pjp")public Object doAround(ProceedingJoinPoint pjp) {Object[] args = pjp.getArgs();Object result;try {// BeforeSystem.out.println(pjp.getSignature().getName() + "环绕前置通知---");result = pjp.proceed(args);// AfterReturningSystem.out.println(pjp.getSignature().getName() + "环绕返回通知---");}catch (Throwable e){// AfterThrowingSystem.out.println(pjp.getSignature().getName() + "环绕异常通知---");throw new RuntimeException(e);}finally {// AfterSystem.out.println(pjp.getSignature().getName() + "环绕最终通知---");}return result;}@After("test()")public void doAfter(JoinPoint joinPoint){System.out.println(joinPoint.getSignature().getName() + "执行最终通知---");}}

添加测试方法:

package com.example.demo.module.aspect;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RequestMapping("test")
@RestController
public class Test {@GetMapping("testMethod")public void testMethod(){System.out.println("方法执行---");}}

运行结果:
在这里插入图片描述

测试异常通知,修改测试方法:

package com.example.demo.module.aspect;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RequestMapping("test")
@RestController
public class Test {@GetMapping("testMethod")public void testMethod(){int i= 1/0;System.out.println("方法执行---");}}

运行结果:
在这里插入图片描述

通知执行顺序

Spring 版本不一样,通知执行顺序可能也会存在差异

@Before、@After、@AfterReturning、@AfterThrowing执行顺序

  1. Spring 4.0
    正常情况:@Before -> 目标方法 -> @After -> @AfterReturning
    异常情况:@Before -> 目标方法 -> @After -> @AfterThrowing
  2. Spring 5.28
    正常情况:@Before -> 目标方法 -> @AfterReturning -> @After
    异常情况:@Before -> 目标方法 -> @AfterThrowing -> @After

@Around的执行顺序

  1. Spring 4.0
    正常情况:环绕前置 -> 目标方法执行 -> 环绕返回 -> 环绕最终
    异常情况:环绕前置 -> 目标方法执行 -> 环绕异常 -> 环绕最终
  2. Spring 5.28
    正常情况:环绕前置 -> 目标方法执行 -> 环绕返回 -> 环绕最终
    异常情况:环绕前置 -> 目标方法执行 -> 环绕异常 -> 环绕最终

五大通知执行顺序

  1. Spring 4.0
    正常情况:环绕前置 -> @Before -> 目标方法执行 -> 环绕返回 -> 环绕最终 -> @After -> @AfterReturning
    异常情况:环绕前置 -> @Before -> 目标方法执行 -> 环绕异常 -> 环绕最终 -> @After -> @AfterThrowing
  2. Spring 5.28
    正常情况:环绕前置 -> @Before -> 目标方法执行 -> @AfterReturning -> @After -> 环绕返回 -> 环绕最终
    异常情况:环绕前置 -> @Before -> 目标方法执行 -> @AfterThrowing -> @After -> 环绕异常 -> 环绕最终
http://www.dtcms.com/a/614545.html

相关文章:

  • 千博企业网站管理系统完整版 2014ios认证 东莞网站建设
  • 国外的一些网站精美网站设计欣赏
  • 深度学习:动量梯度下降实战(Momentum Gradient Descent)
  • 电脑做服务器建网站app试玩网站制作
  • 【Janet】数据结构
  • Tensor与NumPy转换
  • 06-文件操作-教程
  • 【ros2】ROS2 C++服务端与客户端开发指南
  • 网站开发成本主要有哪些网络广告发布
  • 【035】Dubbo3从0到1系列之dubbo-remoting核心接口Endpoint
  • 用备份的网站代码做网站步骤小程序模板怎么导入
  • 利用帝国cms网站建设网页源代码怎么搜索关键词
  • 【愚公系列】《腾讯元宝从入门到精通》002-提示词设计与优化
  • C++哈希(包含unordered_set和unordered_map的封装)
  • 编译类语言 | 深入了解编译原理及应用
  • 西安建设银行网站专业公司网站 南通
  • 禁止同ip网站查询抖音小程序注册
  • Chaos-nano:Arduino Pro Mini 轻量级操作系统解析与实战应用
  • 从 0 到 1 学爬虫:Python 基础语法在爬虫中的实战运用用这个标题写一篇文章,在当前对话窗口输出
  • 网页设计网站排行榜中国发达国家还有多远
  • Podman和Docker
  • 自己架设服务器做网站一同看网页打不开
  • 手写LRU 缓存
  • 23-MD5+DES+Webpack:考试宝
  • 前端构建工具环境变量,安全管理
  • 个人网站名称举例网站建设需要的硬件
  • 数据分析笔记09:Python条件语循环
  • 可在哪些网站做链接赣州网络招聘
  • 高端网站制作网站背景自动切换
  • 代码生成工具GitHub Copilot介绍