![](/img/trans.png)
[英]Aspect using pointcut=@annotation for @AfterThrowing is running twice
[英]Aspect pointcut to use annotation property
有一个TraceAspect,它应该在任何使用Trace注释注释的方法或类(类的所有方法)上执行一些日志记录。
@Aspect
public class TraceAspect {
@Pointcut("(@annotation(Trace) || @within(Trace)) && execution(* *(..))")
void allAnnotated(){}
@Around("allAnnotated()")
public Object trace(final ProceedingJoinPoint joinPoint) throws Throwable {
// doing some stuff here
}
}
和注释:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
boolean enabled() default true;
}
我需要修改切入点,以便将具有Trace.enabled设置为false的所有方法/类都跳过(不视为连接点)。
@Pointcut("allAnnotated() && Trace.enabled")
或者(如果不可能的话)至少在通知中包含该注释及其值,以便我可以检查属性并跳过日志记录...
在https://eclipse.org/aspectj/doc/released/README-160.html中查看“注释值匹配”
您可以做的是:
@Pointcut("execution(@Trace(enabled=true) * *(..)) || execution(* (@Trace(enabled=true) *).*(..))")
第一个是方法级别的注释,第二个是类型级别的注释。 在@ annotation / @ within中嵌入值的语法尚不存在(因此您无法执行@annotation(Trace(enabled=true))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.