簡體   English   中英

使用注釋屬性的方面切入點

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM