Spring不同版本Aop执行顺序差异

4 篇文章 0 订阅

Spring不同版本Aop执行顺序差异

前言

Spring升级过程过发现Spring4和Spring5版本之间的AOP差异是比较大的,
Spring4的执行顺序是:

正常情况: @Around前环绕通知–>@Before–>@Around后环绕通知–>@After–>@AfterReturning
异常情况:@Around前环绕通知–>@Before–>@After–>@AfterThrowing

Spring5的执行顺序是:

正常情况:@Around前环绕通知–>@Before–>@AfterReturning–>@After–>@Around后环绕通知
异常情况:@Around前环绕通知–>@Before–>@AfterThrowing–>@After

刨了Spring的版本修改记录发现在Spring-5.2.7.RELEASE版本调整了aop的执行顺序:
在这里插入图片描述

https://github.com/spring-projects/spring-framework/issues/25186

下面就撸代码测试一把:

Spring5的

maven依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

上代码

public interface EmployeeService {
    public int fun(int x,int y);
}

@Service
public class EmployeeServiceImpl implements EmployeeService{
    @Override
    public int fun(int x, int y) {
        int result = x / y;
        System.out.println("业务代码被执行了");
        return result;
    }
}

@Aspect
@Component
public class EmployeeAspect {

    @Before("execution(public int com.pandamig.aop.EmployeeServiceImpl.*(..))")
    public void before() {
        System.out.println("@Before");
    }

    @After("execution(public int com.pandamig.aop.EmployeeServiceImpl.*(..))")
    public void after() {
        System.out.println("@After");
    }

    @AfterReturning("execution(public int com.pandamig.aop.EmployeeServiceImpl.*(..))")
    public void afterReturning() {
        System.out.println("@AfterReturning");
    }

    @AfterThrowing("execution(public int com.pandamig.aop.EmployeeServiceImpl.*(..))")
    public void afterThrowing() {
        System.out.println("@AfterThrowing");
    }

    @Around("execution(public int com.pandamig.aop.EmployeeServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object retvalue = null;
        System.out.println("@Around 前");
        retvalue = proceedingJoinPoint.proceed();
        System.out.println("@Around 后");
        return retvalue ;
    }
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class EmployeeServiceImplTest {

    @Resource
    EmployeeService employeeService;

    @Test
    public void funTest(){
        System.out.println("Spring:"+SpringVersion.getVersion()+"--SpringBoot:"+ SpringBootVersion.getVersion());
        System.out.println();

        employeeService.fun(10,2);
    }
}

上测试结果

Spring:5.2.8.RELEASE--SpringBoot:2.3.3.RELEASE

@Around 前
@Before
业务代码被执行了
@AfterReturning
@After
@Around 后

把pom中的Springboot版本修改为1.5.9.Release看看效果是什么样的:

Spring:4.3.13.RELEASE--SpringBoot:1.5.9.RELEASE
@Around 前
@Before
业务代码被执行了
@Around 后
@After
@AfterReturning

小结

Spring5的aop执行顺序更符合常规逻辑:

    // @Around
    public void test(){
        try {
            //  @Before
            // 业务代码
            // @AfterReturning
        }catch (Exception e){
            // @AfterThrowing
        }finally {
            // @After
        }
    }
    // @Around
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

pandamig

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

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

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

打赏作者

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

抵扣说明:

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

余额充值