gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:47:52 25 4
gpt4 key购买 nike

我们使用自定义注释实现“之前”建议,以便仅在(对此问题不感兴趣的)业务逻辑适用时执行某些方法。

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

调试它,我看到Cglib2AopProxy$CglibMethodInitation.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() {
....
}

这看起来很普通,但显然我做错了什么。任何关于如何每次调用只执行一次建议的建议将不胜感激。

最佳答案

您应该打印 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 * *.*(..)))

关于java - 在建议被执行两次之前...相同的连接点为相同的方法列出了两次,因此它被调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com