【发布时间】: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 id和aop:pointcut id使用相同的 ID。我怀疑这可能是问题所在。您可能想尝试使用唯一 ID。 -
@MadhusudanaReddySunnapu - 我注意到相同的 aop:aspect id 并尝试更改它但没有奏效。我错过了同样相同的 aop:pointcut id,更改后问题得到解决。谢谢指出!!
标签: spring-aop aspect pointcut spring-aspects