简体   繁体   中英

Before advice getting executed twice…same join point listed twice for same method so it gets called twice

We're implemented "before" advice using custom annotations so as to only execute certain methods if the (uninteresting to this problem) business logic applies.

We're seeing the aspect called twice for each invocation of the method.

Debugging into it I see Cglib2AopProxy$CglibMethodInvocation.proceed has an array called: interceptorsAndDynamicMethodMatchers . This array lists our PointCut ("RequiresX") twice.

Here is the join point:

@Before(@annotation(requiresX)”)
public Object process(ProceedingJoinPoint joinPoint, RequiresACL requiresX) throws Throwable
{
    Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
    log.info(" method:" + method.getName());

    // do business logic of the aspect…

    log.info(" joinPoint.proceed with call to " + method.getName());
 }

and here is our custom annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.Method)
public @interface RequiresX {
}

and here is a method we can to intercept:

@RequiresX()
public String someMethod() {    
    ....
}

This seems pretty vanilla but clearly I've done something wrong. Any suggestions on how to only execute the advice once per call would be greatly appreciated.

you should print Pointcut and see the problem like:

@Before(@annotation(requiresX)”)
public Object process(ProceedingJoinPoint joinPoint, RequiresACL requiresX) throws Throwable
{

    log.info(" pointcut:" + joinPoint.getKind());
    // do business logic of the aspect…


 }

it will print the problem like:

pointcut:method-call 
pointcut:method-execution

So, you should change Pointcut as:

@Before(@annotation(RequiresX) && execution(@RequiresX * *.*(..)))

We found the answer both via trial and error and via this post: http://forum.spring.io/forum/spring-projects/aop/25729-advice-is-called-twice-with-aop-auto-proxy-configuration

Bottom line is we had an @Aspect annotation on the aspect class as well as specifying the aspect in the Spring XML file. Don't do both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM