Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP advice is called twice

Tags:

aop

spring-aop

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.

like image 945
nb.alexiev Avatar asked Oct 26 '11 09:10

nb.alexiev


People also ask

What is Spring AOP advice?

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.

What will after advice do in Spring AOP?

After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation: import org. aspectj.

What is pointcut and advice in spring?

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 Answers

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.

like image 64
davorp Avatar answered Dec 28 '22 07:12

davorp