在建议执行两次之前...相同的连接点针对相同的方法列出了两次,因此它被调用了两次

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

我们使用自定义注释实现了 "before" 建议,以便仅在(对这个问题不感兴趣的)业务逻辑适用时才执行某些方法。

我们看到每次调用该方法都会调用两次方面。

调试它我看到 Cglib2AopProxy$CglibMethodInvocation.proceed 有一个名为:interceptorsAndDynamicMethodMatchers 的数组。这个数组列出了我们的 PointCut ("RequiresX") 两次。

这里是连接点:

@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());
 }

这是我们的自定义注释

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

这是我们可以拦截的方法:

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

这看起来很普通,但显然我做错了什么。非常感谢任何有关如何每次调用只执行一次建议的建议。

我们通过反复试验和这个 post 找到了答案:http://forum.spring.io/forum/spring-projects/aop/25729-advice-is-called-twice-with-aop-auto-proxy-configuration

底线是我们在方面 class 上有一个 @Aspect 注释,并在 Spring XML 文件中指定了方面。不要两者都做。

您应该打印 Pointcut 并查看如下问题:

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

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


 }

它将打印如下问题:

pointcut:method-call 
pointcut:method-execution

因此,您应该将切入点更改为:

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