【问题标题】:Spring AOP is being invoked unexpectedlySpring AOP 被意外调用
【发布时间】:2016-03-14 08:04:14
【问题描述】:

我在我们的应用程序中为 2 个不同的包配置了 Spring AOP 来记录异常。 每个包有 2 种不同的配置:

<aop:config>
    <aop:aspect id="aspectLoggging" ref="abcExceptionAspect">
        <aop:pointcut id="pointCut"
            expression="execution(* com.abc.*.*(..))" />
        <aop:before method="logBefore" pointcut-ref="pointCut" />
        <aop:after-throwing method="logExceptionABC"
            throwing="error" pointcut-ref="pointCut" />
        <aop:after method="logAfter" pointcut-ref="pointCut" />
    </aop:aspect>
</aop:config>

<aop:config>
    <aop:aspect id="aspectLoggging" ref="xyzlogAspect">
        <aop:pointcut id="pointCut"
            expression="execution(* com.xyz.*.*(..))" />
        <aop:before method="logBefore" pointcut-ref="pointCut" />
        <aop:after method="logAfter" pointcut-ref="pointCut" />
        <aop:after-throwing method="logExceptionXYZ"
            throwing="error" pointcut-ref="pointCut" />
    </aop:aspect>
</aop:config>

在服务方法调用中,会调用属于这些包中的每一个的类的方法:

公共无效方法() {

方法1(); -> 包 abc

方法2(); -> 包 xyz

}

在调用 logExceptionXYZ 方法的方法 2 中发生了一些异常,我们将它包装在一个通用异常中,比如 ExceptionXYZ,然后进一步抛出它。

但是在这之后,logExceptionABC 方法也会被调用并抛出一个通用异常,比如 ExceptionABC。

我无法理解为什么会调用 logExceptionABC 方法?

如果有人知道这样的问题,请告诉我!

问候, 拉胡尔

【问题讨论】:

  • aop:aspect idaop:pointcut id 使用相同的 ID。我怀疑这可能是问题所在。您可能想尝试使用唯一 ID。
  • @MadhusudanaReddySunnapu - 我注意到相同的 aop:aspect id 并尝试更改它但没有奏效。我错过了同样相同的 aop:pointcut id,更改后问题得到解决。谢谢指出!!

标签: spring-aop aspect pointcut spring-aspects


【解决方案1】:

相同的id 被分配给两个aop:aspect 标签。 aop:pointcut 标签也是如此。

尝试分配唯一的 ID。

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2012-07-27
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多