首页 新闻 赞助 找找看

Spring AOP的执行顺序问题

0
悬赏园豆:100 [待解决问题]

我在一个切入点上同时配置了5种类型的通知,发现网上说的五种通知的执行顺序是错的,网上说五种通知的执行顺序如下图:

下面我不修改任何代码,仅仅修改xml配置通知的顺序
xml配置1

<aop:aspect id="advice" ref="myAdvice">
            <!--            切入前置通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <!--            切入环绕通知-->
            <aop:around method="around" pointcut-ref="point"/>
<!--            切入最终通知-->
            <aop:after method="after" pointcut-ref="point"/>
<!--            切入返回通知-->
            <aop:after-returning method="afterReturn" pointcut-ref="point"/>
<!--            切入异常通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="point"/>
        </aop:aspect>

输出结果为:

before........
around1........
add......
around1........
after........
afterReturn........

xml配置2:

<aop:config>
<!--        配置切入点-->
        <aop:pointcut id="point" expression="execution(* com.hzq.service.*.*(..))"/>
<!--        切入通知-->
        <aop:aspect id="advice" ref="myAdvice">
<!--            切入最终通知-->
            <aop:after method="after" pointcut-ref="point"/>
<!--            切入返回通知-->
            <aop:after-returning method="afterReturn" pointcut-ref="point"/>
<!--            切入异常通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="point"/>
<!--            切入环绕通知-->
            <aop:around method="around" pointcut-ref="point"/>
<!--            切入前置通知-->
            <aop:before method="before" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
around1........
before........
add......
after........
afterReturn........
around1........

我发现AOP执行的顺序好像和切面的配置顺序有关,有没有大佬解释一下为什么会出现这种情况?

夜雨夕眠的主页 夜雨夕眠 | 初学一级 | 园豆:102
提问于:2021-05-18 18:04
< >
分享
所有回答(3)
0
智客工坊 | 园豆:1855 (小虾三级) | 2021-05-21 16:08
0

可能跟版本有关系,我的spring版本里,advice执行链的第一个MethodInterceptor永远是around的,然后是before,after,在Around里回调方法时,就会调后边的advice了,这就导致永远是
around before
before
processing-----
after
around after
这样

The_MIST | 园豆:202 (菜鸟二级) | 2022-02-11 14:01
0

你的是对的,网上版本也是对的。
区别在于是spring 5.2.7.RELEASE之前还是之后。
spring 5.2.7.RELEASE之前是 环绕前->前置->目标方法->环绕后->后置。
之后就是你说的这种。

chxLonely | 园豆:202 (菜鸟二级) | 2023-02-16 22:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册