I have the following Spring AOP advice and I can't find out why it is called twice:
@Component
@Aspect
public class LoggingAspects {
Logger logger = LoggerFactory.getLogger(LoggingAspects.class);
@AfterReturning(pointcut = "execution(public * com.A.B.C.service.impl.*.browse(..))",
returning = "retVal")
public Object onBrowse(DomainClass retVal) {
logger.info("#######################Advice Called: +retVal);
return null;
}
}
The configuration is as simple as that:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="loggingCASAspect" class="com.minervanetworks.xtv.stb.service.aspects.LoggingCASAspects"/>
I have also tried the following advice with the same result (called twice).
@AfterReturning(pointcut="@annotation(com.A.B.C.service.impl.LOG)", returning="retVal")
public Object onBrowse(JoinPoint jp, DomainClass retVal) {
logger.info("#######################Advice called! " + jp.toLongString()
+ " Target: " + jp.getTarget()
+ " Signature: " + jp.getSignature()
+ " Kind: " + jp.getKind()
+ " This: " + jp.getThis()
+ " Source Location: " + jp.getSourceLocation());
return null;
}
The debug info from the above logger is:
2011-10-26 11:56:01,887 [INFO][com.A.B.C.service.aspects.LoggingAspects] #######################Advice called! execution(public abstract com.A.B.C.domain.DomainClass com.A.B.C.service.ContentManager.browse(java.lang.String,java.lang.String,java.lang.String,java.lang.Boolean)) Target: com.A.B.C.service.impl.ContentManagerImpl@62ad191 Signature: DomainClass com.A.B.C.service.ContentManager.browse(String,String,String,Boolean) Kind: method-execution This: com.A.B.C.service.impl.ContentManagerImpl@62ad191 Source Location: org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@d324de2
It is displayed twice with exactly the same values.
In Spring AOP a join point is always the execution of a method. Advice: Advices are actions taken for a particular join point. In terms of programming, they are methods that get executed when a certain join point with matching pointcut is reached in the application.
After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation: import org. aspectj.
Pointcut is a predicate or expression that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut. Spring uses the AspectJ pointcut expression language by default.
1. hint
Do you use JavaConfig or xml? If you're using both, it could be that the Aspect is being processed two times by the Spring IoC container.
2. hint
I don't annotate aspects with @Component annoation, try to remove it, maybe because of that is being processed twice by Spring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With