java - aspectj 不拦截带有注释的方法
问题描述
我试图让 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(* *(..)) && within(@InterceptMe *)")
. 它应该是@Around("execution(* *(..)) && @annotation(your.package.InterceptMe )")
或者,如果您需要从注释中访问某些属性:
@Around("execution(* *(..)) && @annotation(interceptMeVar)")
public Object doIntercept(ProceedingJoinPoint proceedingJoinPoint,InterceptMe interceptMeVar)
推荐阅读
- c# - 放置静态api密钥并将其与标头中的请求匹配是一种好方法吗?
- javascript - PWA - 以编程方式检查是否安装在 JavaScript 中
- flutter - Flutter 动态创建的 FilterChip 未在 onSelected() 上更新
- javascript - 如何使用反应钩子使选择下拉默认选择
- javascript - 映射 Immutable.js 记录
- amazon-web-services - 缓解 terraform 0.11.7 上的“限制:超出速率”错误适用于 AWS
- excel - 如何更改此 vba 代码以仅获取自动过滤范围而不是第二行?
- asp.net-core - 如何从服务器端读取本地存储 cookie?
- sql - 我可以在一个语句中从两个表中删除条目吗?
- html - 使用带参数的方法只单击一次按钮?