gpt4 book ai didi

java - spectj 不拦截带有注释的方法

转载 作者:行者123 更新时间:2023-12-02 10:30:40 25 4
gpt4 key购买 nike

我试图让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(){

}
}

为了简洁起见,我删除了 !within(InterceptMeAspect),但无论如何它并没有拦截太多。如果我删除注释约束(在(@InterceptMe *)内),它可以工作,但会拦截所有内容并导致一个大问题。

输出字节码似乎具有完整的注释,因此我希望注释标准匹配。我正在做或尝试做编译时编织。这很重要,因为我还有另一个方面可以使用上面相同的方法来工作。我怀疑这个方面搞乱了这个,但最终的字节码不应该有注释,对吗?

编辑:这是另一方面的代码:

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

我有一个通用日志记录方面和一个特殊的上下文日志记录方面。我猜这也写得不正确,应该遵循上面的格式。

最佳答案

而不是@Around("execution(* *(..)) && inside(@InterceptMe *)")。它应该是 @Around("execution(* *(..)) && @annotation(your.package.InterceptMe )")

或者,如果您需要从注释访问某些属性:

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

关于java - spectj 不拦截带有注释的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631713/

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