Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在建议执行两次之前…同一个连接点为同一个方法列出两次,所以它会被调用两次_Java_Aspectj_Aop_Aspect - Fatal编程技术网

Java 在建议执行两次之前…同一个连接点为同一个方法列出两次,所以它会被调用两次

Java 在建议执行两次之前…同一个连接点为同一个方法列出两次,所以它会被调用两次,java,aspectj,aop,aspect,Java,Aspectj,Aop,Aspect,我们使用自定义注释实现了“之前”建议,以便仅在(对这个问题不感兴趣的)业务逻辑适用时执行某些方法 我们看到,每次调用该方法都会调用两次方面 调试到它中我看到了Cglib2AopProxy$CglibMethodInvocation.continue有一个名为:拦截器和dynamicmethodmatchers的数组。这个数组列出了我们的切入点(“RequiresX”)两次 以下是连接点: @Before(@annotation(requiresX)”) public Object process

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

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

调试到它中我看到了
Cglib2AopProxy$CglibMethodInvocation.continue
有一个名为:
拦截器和dynamicmethodmatchers
的数组。这个数组列出了我们的
切入点(“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() {    
    ....
}

这看起来很普通,但很明显我做错了什么。对于如何在每次通话中只执行一次建议,我们将不胜感激。

我们通过尝试和错误以及本帖找到了答案:


底线是我们在Aspect类上有一个@Aspect注释,并在SpringXML文件中指定了Aspect。不要同时做这两件事。

您应该打印切入点,并看到如下问题:

@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)”)
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 * *.*(..)))