AOP中遇到的问题

在复习AOP的时候发现了一个问题

在使用xml方式配置AOP时候发现了Spring的后置通知和最终通知的顺序问题

正常执行的顺序应该是:
前置通知(before)>>目标方法>>后置通知(after-returning)>>异常通知(after-throwing)>>最终通知(after)
当然后置通知和异常通知只能有一个执行

我在xml的编写顺序没有按照平常那么来,是这么写的,先写的最终通知,最后写的后置通知

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="accountService" class="com.ligong.service.impl.AccountServiceImpl"/>
    <bean id="aspect" class="com.ligong.utils.Aspect"/>
    <!--配置aop-->
    <aop:config>
        <!--全局切入点,可以被多个切面的通知所引用-->
        <!--<aop:pointcut id="globalPointcut" expression="execution(* *..*.*(..))"/>-->
        <!--配置切面-->
        <aop:aspect id="myAspect" ref="aspect">
            <!--配置切入点-->
            <aop:pointcut id="myPointcut" expression="execution(public * com.ligong.service.impl.*.*(..))"/>
            <!--配置的通知的类型,建立通知方法与切入点的关联-->
            <!--配置前置通知-->
            <aop:before method="printLogBefore" pointcut-ref="myPointcut"/>
            <!--配置最终通知-->
            <aop:after method="printLogAfter" pointcut-ref="myPointcut"/>
            <!--配置后置通知-->
            <aop:after-returning method="printLogAfterReturning" pointcut-ref="myPointcut"/>
            

            <!--配置异常通知-->
            <!--<aop:after-throwing method="printLogThrowing" pointcut-ref="myPointcut"/>-->
        </aop:aspect>
    </aop:config>
</beans>

但是按常理来说他执行的顺序应该是这样的
前置通知(before)>>目标方法>>后置通知(after-returning)>>最终通知(after)

ps:下面这个图和我写的代码没啥关系,只是列出这几个通知的先后顺序

在这里插入图片描述
finally中的方法应该是最后执行的
但是我的执行顺序是这样
前置通知(before)>>目标方法>>最终通知(after)>>后置通知(after-returning),后置通知和最终通知的顺序反了
在这里插入图片描述
然后在xml中,把最终通知移到后置通知下面就好了
难道Spring AOP中后置通知和最终通知的执行顺序与XML中通知的先后顺序有关?

应该也是有争议的
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值