深入解析Spring AOP切面优先级与执行顺序

目录

引言

1、Spring AOP的执行流程

1.1 切面的优先级

1.2 调用链的执行

2、源码分析

2.1 ReflectiveMethodInvocation类

2.2  proceed()方法的执行逻辑

2.3. 切面的执行顺序 

2.4 参考代码示例

3、总结


引言

在Spring AOP(面向切面编程)中,切面(Aspect)是用于在方法执行前后插入额外逻辑的机制。当多个切面作用于同一个方法时,它们的执行顺序由切面的优先级决定。本文将从一个具体的场景出发,探讨当两个切面(Aspect A和Aspect B)同时作用于同一个方法时,它们的执行顺序以及背后的原理。我们将结合JDK 相关源码,深入分析Spring AOP的实现机制。

问题描述

假设我们有两个切面A和B,它们都作用于同一个Controller层的方法。切面A的优先级高于切面B,且两个切面中都调用了joinPoint.proceed()方法。测试发现,切面A执行proceed()后,程序并没有直接进入Controller层,而是进入了切面B的处理逻辑,最后才进入Controller层。为什么会这样呢?

1、Spring AOP的执行流程

要理解这个问题,我们需要先了解Spring AOP的执行流程。Spring AOP是基于代理模式实现的,当一个方法被多个切面拦截时,Spring会将这些切面按照优先级排序,形成一个调用链。每个切面在调用proceed()时,实际上是在调用链中继续执行下一个切面或目标方法。

1.1 切面的优先级

切面的优先级可以通过@Order注解或实现Ordered接口来指定。数值越小,优先级越高。在我们的场景中,切面A的优先级高于切面B。如果切面上没有@Order注解,则默认为最低优先级(数值越大,优先级越低)。

public class OrderComparator implements Comparator<Object> {
   protected int getOrder(@Nullable Object obj) {
        if (obj != null) {
            Integer order = this.findOrder(obj);
            if (order != null) {
                return order;
            }
        }

        return Integer.MAX_VALUE;
    }
    ...
}

1.2 调用链的执行

当目标方法被调用时,Spring会按照切面的优先级顺序依次执行切面的前置逻辑(@Before),然后调用proceed()方法。proceed()方法会继续执行调用链中的下一个切面或目标方法。当所有切面的前置逻辑执行完毕后,最终会调用目标方法(即Controller层的方法)。

2、源码分析

为了更好地理解这一过程,我们可以从Spring AOP的源码入手,分析proceed()方法的执行逻辑。

2.1 ReflectiveMethodInvocation

Spring AOP的核心逻辑位于ReflectiveMethodInvocation类中。该类负责管理切面的调用链。以下是ReflectiveMethodInvocation类的简化代码:

public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable {

    protected final List<?> interceptorsAndDynamicMethodMatchers;
    private int currentInterceptorIndex = -1;
    
    protected ReflectiveMethodInvocation(Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments, @Nullable Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
        this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
        // 其他初始化逻辑
    }

    public Object proceed() throws Throwable {
        // 如果所有拦截器都执行完毕,则调用目标方法
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return this.invokeJoinpoint();
        } else {
        // 获取下一个拦截器并执行
            Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
            if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
                InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
                Class<?> targetClass = this.targetClass != null ? this.targetClass : this.method.getDeclaringClass();
                return dm.matcher().matches(this.method, targetClass, this.arguments) ? dm.interceptor().invoke(this) : this.proceed();
            } else {
                return ((MethodInterceptor)interceptorOrInterceptionAdvice).invoke(this);
            }
        }
    }


    protected Object invokeJoinpoint() throws Throwable {
        // 调用目标方法
        return AopUtils.invokeJoinpointUsingReflection(this.target, this.method, this.arguments);
    }

}

2.2  proceed()方法的执行逻辑

  • proceed()方法会检查当前拦截器的索引是否已经到达调用链的末尾。如果是,则调用目标方法(invokeJoinpoint())。

  • 如果还有未执行的拦截器,proceed()会获取下一个拦截器并执行其invoke()方法。

2.3. 切面的执行顺序 

在我们的场景中,切面A和切面B都会被封装为MethodInterceptor,并按照优先级排序。切面A的优先级高于切面B,因此切面A会先执行。当切面A调用proceed()时,Spring会继续执行切面B的逻辑。切面B执行完毕后,才会调用目标方法。 

为什么切面A的proceed()会进入切面B?

根据上述源码分析,proceed()方法并不是直接调用目标方法,而是继续执行调用链中的下一个切面。因此,当切面A调用proceed()时,Spring会继续执行切面B的逻辑,而不是直接进入Controller层。

2.4 参考代码示例

以下是一个简单的代码示例,展示了两个切面A和B的执行顺序:

@Aspect
@Component
@Order(1) // 切面A的优先级更高
public class AspectA {

    @Before("execution(* cn.ygc.demo.controller.*.*(..))")
    public void beforeA(JoinPoint joinPoint) {
        System.out.println("AspectA: Before method execution");
    }

    @Around("execution(* cn.ygc.demo.controller.*.*(..))")
    public Object aroundA(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("AspectA: Around before proceed");
        Object result = joinPoint.proceed();
        System.out.println("AspectA: Around after proceed");
        return result;
    }
}

@Aspect
@Component
@Order(2) // 切面B的优先级较低
public class AspectB {

    @Before("execution(* cn.ygc.demo.controller.*.*(..))")
    public void beforeB(JoinPoint joinPoint) {
        System.out.println("AspectB: Before method execution");
    }

    @Around("execution(* cn.ygc.demo.controller.*.*(..))")
    public Object aroundB(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("AspectB: Around before proceed");
        Object result = joinPoint.proceed();
        System.out.println("AspectB: Around after proceed");
        return result;
    }
}

@RestController
@RequestMapping("/api/demo")
public class TestController {

    @GetMapping("/test")
    public String test() {
        System.out.println("Controller: Method executed");
        return "Hello World";
    }
}

运行结果:

AspectA: Around before proceed
AspectA: Before method execution
AspectB: Around before proceed
AspectB: Before method execution
Controller: Method executed
AspectB: Around after proceed
AspectA: Around after proceed

3、总结

通过本文的分析,我们深入理解了Spring AOP中切面优先级与执行顺序的机制。希望这篇文章能帮助你更好地掌握Spring AOP的工作原理。

告别重复劳动!基于注解的通用列表导出组件设计与实现-CSDN博客

了解idea插件的开发流程及idea右键选择项目批量导出插件介绍-CSDN博客

深入讲解TransmittableThreadLocal工作原理,并手写一个精简版的功能组件-CSDN博客

如何快速实现一个简单的通用缓存工具?-CSDN博客

java实现接口反参JsonData<T>封装,并实现字符串与泛型对象JsonData<T>之间的快速转换-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网页打不开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值