Search code examples
spring-aopaspectpointcutspring-aspects

Spring AOP is being invoked unexpectedly


I have configured Spring AOP for 2 different packages in our application to log exceptions. There are 2 different configurations for each package:

<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>

In a service method call, there are calls to the methods of the classes belonging to each of these packages:

public void method() {

method1(); -> package abc

method2(); -> package xyz

}

Some exception occurs in method2 which invokes logExceptionXYZ method where we are wrapping it in a generic exception, say ExceptionXYZ, and throwing it further.

But some how after this, the logExceptionABC method also gets invoked and throws a generic exception , say ExceptionABC.

I'm not able to understand as why logExceptionABC method is getting invoked?

Please let me know if someone knows about such an issue!

Regards, Rahul


Solution

  • Same id is being assigned to both the aop:aspect tags. Similar is the case with the aop:pointcut tags as well.

    Try with assigning unique IDs.