aop:config内的标签书写顺序是否有要求?

新手上路,请多包涵

题目描述

使用Spring AOP时发现在<aop:config>定义<aop:pointcut>和<aop:aspect>顺序不正确会造成报错。

题目来源及相关代码

下面xml中使用到的bean均已注册。
如下代码写不报错:

<aop:config>
    <aop:pointcut id="performence" expression="execution(* com.Leslie.pojo.performer.singer.perform())
                                               and bean(duke)"/>
    

    <aop:pointcut id="think" expression="execution(* com.Leslie.pojo.Volunteer.think(String))
    and args(thought)
    and bean(hank)"/>

    <aop:aspect ref="audience">
        <aop:before method="takeSeats" pointcut-ref="performence"/>
        <aop:before method="turnOffPhone" pointcut-ref="performence"/>
        <aop:after-returning method="applaud" pointcut-ref="performence"/>
        <aop:after-throwing method="demanRefund" pointcut-ref="performence"/>
    </aop:aspect>
    
    <aop:aspect ref="james">
        <aop:before method="guessThought" pointcut-ref="think" arg-names="thought"/>
    </aop:aspect>
</aop:config>

如下代码写报错:

<aop:config>
    <aop:pointcut id="performence" expression="execution(* com.Leslie.pojo.performer.singer.perform())
                                               and bean(duke)"/>

    <aop:aspect ref="audience">
        <aop:before method="takeSeats" pointcut-ref="performence"/>
        <aop:before method="turnOffPhone" pointcut-ref="performence"/>
        <aop:after-returning method="applaud" pointcut-ref="performence"/>
        <aop:after-throwing method="demanRefund" pointcut-ref="performence"/>
    </aop:aspect>

    <aop:pointcut id="think" expression="execution(* com.Leslie.pojo.Volunteer.think(String))
    and args(thought)
    and bean(hank)"/>

    <aop:aspect ref="james">
        <aop:before method="guessThought" pointcut-ref="think" arg-names="thought"/>
    </aop:aspect>
</aop:config>

报错信息如下图:
image.png

是不是定义顺序的问题呢?Spring AOP对标签定义顺序有什么要求呢?

阅读 606
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题