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

【Spring Boot AOP通知顺序】

文章目录

  • 一、Spring Boot AOP简介
  • 二、通知顺序
    • 1. 通知类型及其顺序
      • 示例代码
    • 2. 控制通知顺序
      • 示例代码


一、Spring Boot AOP简介

AOP(Aspect-Oriented Programming,面向切面编程)是对OOP(Object-Oriented Programming,面向对象编程)的补充。AOP通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。

在Spring Boot中,AOP主要通过注解和AspectJ来实现。主要的AOP注解有:

  • @Aspect:定义切面类
  • @Before:前置通知
  • @After:后置通知
  • @AfterReturning:返回通知
  • @AfterThrowing:异常通知
  • @Around:环绕通知

二、通知顺序

1. 通知类型及其顺序

在Spring AOP中,通知按以下顺序执行:

  1. @Around(环绕通知)前半部分
  2. @Before(前置通知)
  3. 被代理的方法执行
  4. @AfterReturning(返回通知)或@AfterThrowing(异常通知)
  5. @After(后置通知)
  6. @Around(环绕通知)后半部分

示例代码

@Aspect
@Component
public class LoggingAspect {@Before("execution(* com.example.service.*.*(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("logBefore() is running!");}@After("execution(* com.example.service.*.*(..))")public void logAfter(JoinPoint joinPoint) {System.out.println("logAfter() is running!");}@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {System.out.println("logAfterReturning() is running!");}@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "error")public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {System.out.println("logAfterThrowing() is running!");}@Around("execution(* com.example.service.*.*(..))")public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("logAround() before is running!");Object result = joinPoint.proceed();System.out.println("logAround() after is running!");return result;}
}

2. 控制通知顺序

在不同的切面之间定义通知的执行顺序。可以使用@Order注解。

示例代码

@Aspect
@Order(1)
@Component
public class FirstAspect {@Before("execution(* com.example.service.*.*(..))")public void beforeAdvice() {System.out.println("FirstAspect beforeAdvice()");}
}@Aspect
@Order(2)
@Component
public class SecondAspect {@Before("execution(* com.example.service.*.*(..))")public void beforeAdvice() {System.out.println("SecondAspect beforeAdvice()");}
}

FirstAspectbeforeAdvice会先于SecondAspectbeforeAdvice执行。

http://www.lryc.cn/news/388493.html

相关文章:

  • k8s是什么
  • 使用雪花算法(Snowflake Algorithm)在Python中生成唯一ID
  • Docker期末复习
  • DP:子数组问题
  • [Day 20] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
  • Handling `nil` Values in `NSDictionary` in Objective-C
  • 【深入浅出 】——【Python 字典】——【详解】
  • 开发RpcProvider的发布服务(NotifyService)
  • Suno: AI音乐创作的新时代
  • 六西格玛项目实战:数据驱动,手机PCM率直线下降
  • 数据结构递归(01)汉诺塔经典问题
  • 计算机专业课面试常见问题-计算机网络篇
  • HarmonyOS ArkUi ArkWeb加载不出网页问题踩坑
  • 微信换手机号了怎么绑定新手机号?
  • 64.WEB渗透测试-信息收集- WAF、框架组件识别(4)
  • java.lang.LinkageError: 链接错误的正确解决方法,亲测有效,嘿嘿,有效
  • python最基础
  • Python学习路线图(2024最新版)
  • 66、基于长短期记忆 (LSTM) 网络对序列数据进行分类
  • RabbitMQ消息可靠性等机制详解(精细版三)
  • 88888
  • 深度学习之激活函数
  • OpenStack开源虚拟化平台(一)
  • C++ | Leetcode C++题解之第207题课程表
  • vue3中的自定义指令
  • Postman接口测试工具的原理及应用详解(一)
  • C++ initializer_list类型推导
  • 造一个交互式3D火山数据可视化
  • 【网络安全】一文带你了解什么是【CSRF攻击】
  • 短视频电商源码如何选择