簡體   English   中英

在建議執行兩次之前...相同的連接點列出兩次相同的方法,因此它被調用兩次

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

我們使用自定義注釋實現“之前”建議,以便僅在(對此問題不感興趣)業務邏輯適用時才執行某些方法。

我們看到每次調用方法時調用兩次方面。

調試它我看到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() {    
    ....
}

這看起來很香草,但顯然我做錯了什么。 關於如何每次通話只執行一次建議的任何建議都將不勝感激。

你應該打印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

所以,你應該改變Pointcut為:

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

我們通過試驗和錯誤以及通過這篇文章找到答案: http//forum.spring.io/forum/spring-projects/aop/25729-advice-is-called-twice-with-aop-auto-proxy-組態

底線是我們在方面類上有一個@Aspect注釋,並在Spring XML文件中指定方面。 不要兩者都做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM