【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执行。

相关推荐
后端小张4 小时前
【JAVA 进阶】Spring Cloud 微服务全栈实践:从认知到落地
java·开发语言·spring boot·spring·spring cloud·微服务·原理
李昊哲小课4 小时前
SSM框架完整教程
spring boot·spring·spring cloud
zl9798994 小时前
RabbitMQ-发布确认高级
java·分布式·rabbitmq
灰小猿4 小时前
分布式项目集成TLog实现轻量级日志链路追踪
java·分布式·springcloud·tlog·日志链路追踪
乄bluefox4 小时前
高性能分布式 ID 生成器:基于 Redis Segment 预分配的实践
java·redis·分布式
数据库学啊4 小时前
时序数据库选型
数据库·时序数据库
TDengine (老段)4 小时前
强杀服务、重启系统及断电对 TDengine 影响
运维·服务器·数据库·物联网·时序数据库·tdengine·涛思数据
Jiong-9524 小时前
Java求职面试:谢飞机的奇妙旅程
java·jvm·线程池·多线程·hashmap·juc·arraylist
数据库学啊4 小时前
时序数据库怎么选
数据库·时序数据库
小二·4 小时前
Java核心机制精讲:深入理解 static 关键字与引用传递
java·java-ee