【发布时间】:2016-08-31 11:11:06
【问题描述】:
我们使用自定义注释实现“之前”建议,以便仅在(对此问题不感兴趣)业务逻辑适用时执行某些方法。
我们看到每次调用方法都会调用方面两次。
调试它我看到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() {
....
}
这看起来很普通,但显然我做错了什么。任何关于如何每次调用只执行一次建议的建议将不胜感激。
【问题讨论】: