简体   繁体   中英

Aspect pointcut to use annotation property

There is a TraceAspect which should execute some logging on any method or class (all methods of class) annotated with Trace annotation.

@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
    }
}

and the annotation:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
    boolean enabled() default true;
}

I need to modify pointcut so that all methods/classes that have Trace.enabled setted to false to be skipped (not considered as join points).

@Pointcut("allAnnotated() && Trace.enabled")

or (if its not possible) at least have that Annotation and its value in the advise so that I could check for property and skip logging...

Take a look at 'annotation value matching' in https://eclipse.org/aspectj/doc/released/README-160.html

What you can do is:

@Pointcut("execution(@Trace(enabled=true) * *(..)) || execution(* (@Trace(enabled=true) *).*(..))")

The first is method level annotations, the second is type level annotations. The syntax for embedding values in @annotation/@within isn't there yet (so you can't do @annotation(Trace(enabled=true))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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