简体   繁体   English

AspectJ不会拦截带有注释的方法

[英]aspectj not intercepting methods with annotation

I am trying to get aspectj to intercept annotated methods: 我正在尝试让Aspectj拦截带注释的方法:

@Aspect
    public class InterceptMeAspect {
      @Around("execution(* *(..)) && within(@InterceptMe *)")
      public Object doIntercept(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("intercepted:");
        return proceedingJoinPoint.proceed();
      }
    }

    public class InterceptedExample {
        @InterceptMe
        public void doSomething(){

        }
    }

I removed the !within(InterceptMeAspect) for brevity, but it isn't intercepting too much anyways. 为了简洁起见,我删除了!within(InterceptMeAspect),但是无论如何它并没有拦截太多。 If I remove the annotation constraint (within(@InterceptMe *)), it works, but intercepts everything and makes for a big problem. 如果我删除注释约束(在(@InterceptMe *)之内),它可以工作,但会拦截所有内容并造成很大的问题。

The output bytecode appears to have the annotations intact, so I would expect the annotation criteria to match. 输出字节码似乎具有完整的注释,因此我希望注释条件能够匹配。 I am doing or attempting to do compile-time weaving. 我正在或正在尝试进行编译时编织。 This is important because I have another aspect that does work using the same approach above. 这很重要,因为我还有另一个方面可以使用上面的相同方法来工作。 I suspect that aspect is messing with this one, but the final bytecode shouldn't have the annotation, right? 我怀疑这方面很麻烦,但是最终的字节码不应该带有注释,对吗?

EDIT: This is the code for the other aspect: 编辑:这是另一方面的代码:

@Around(
      "execution(protected * *(..)) && !within(com.walterjwhite.logging..*) && !call(*.new(..)) && within(@ContextualLoggable *) && !within(@NonLoggable *)")

I have a generic logging aspect and a special contextual logging aspect. 我有一个通用的日志记录方面和一个特殊的上下文日志记录方面。 I am guessing this is incorrectly written as well and should be following the format from above. 我猜这也写错了,应该遵循上面的格式。

Instead of @Around("execution(* *(..)) && within(@InterceptMe *)") . 而不是@Around("execution(* *(..)) && within(@InterceptMe *)") It should be @Around("execution(* *(..)) && @annotation(your.package.InterceptMe )") 它应该是@Around("execution(* *(..)) && @annotation(your.package.InterceptMe )")

Or, if you need to access some properties from annotation: 或者,如果您需要访问注释中的某些属性:

@Around("execution(* *(..)) && @annotation(interceptMeVar)")
public Object doIntercept(ProceedingJoinPoint proceedingJoinPoint,InterceptMe interceptMeVar)

As you use a custom annotation, you should use @annotation instead of within: 使用自定义注释时,应使用@annotation而不是在内部:

@Aspect
    public class InterceptMeAspect {
      @Around("execution(* *(..)) && @annotation(com.or.org.package.InterceptMe)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM