【Spring Boot AOP通知顺序】

文章目录

  • [一、Spring Boot AOP简介](#一、Spring Boot AOP简介)
  • 二、通知顺序
    • [1. 通知类型及其顺序](#1. 通知类型及其顺序)
    • [2. 控制通知顺序](#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(环绕通知)后半部分

示例代码

java 复制代码
@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注解。

示例代码

java 复制代码
@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执行。

相关推荐
艾菜籽6 分钟前
Spring MVC练习:留言板
java·spring·mvc
摇滚侠12 分钟前
Spring Boot 3零基础教程,WEB 开发 自定义静态资源目录 笔记31
spring boot·笔记·后端·spring
摇滚侠13 分钟前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 遍历 笔记40
spring boot·笔记·thymeleaf
派可数据BI可视化21 分钟前
商业智能BI 浅谈数据孤岛和数据分析的发展
大数据·数据库·数据仓库·信息可视化·数据挖掘·数据分析
左灯右行的爱情22 分钟前
4-Spring SPI机制解读
java·后端·spring
Code小翊23 分钟前
C语言bsearch的使用
java·c语言·前端
yong999023 分钟前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
好记忆不如烂笔头abc24 分钟前
linux系统记录登录用户的所有操作
java·linux·服务器
sp421 小时前
一套清晰、简洁的 Java AES/DES/RSA 加密解密 API
java·后端
野犬寒鸦1 小时前
从零起步学习MySQL || 第五章:select语句的执行过程是怎么样的?(结合源码深度解析)
java·服务器·数据库·后端·mysql·adb