I'm using Spring AOP for logging. I want to create a pointcut that applies to all methods except those that have a specific annotation, but I have no idea how to go about it. All I've found is how to include methods with an annotation.
Sample annotation:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface NoLogging {}
Driver Application:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) throws Exception {
foo();
bar();
zot();
baz();
}
@NoLogging public static void foo() {}
public static void bar() {}
@NoLogging public static void zot() {}
public static void baz() {}
}
Aspect in native AspectJ syntax:
package de.scrum_master.aspect;
import de.scrum_master.app.NoLogging;
public aspect MyAspect {
before() : execution(* *(..)) && !@annotation(NoLogging) {
System.out.println(thisJoinPoint);
}
}
Aspect in @AspectJ syntax (should also work in Spring AOP):
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspectX {
@Before("execution(* *(..)) && !@annotation(de.scrum_master.app.NoLogging)")
public void logExceptAnnotated(JoinPoint thisJoinPoint) throws Throwable {
System.out.println(thisJoinPoint);
}
}
Both aspects are equivalent and yield the following output:
execution(void de.scrum_master.app.Application.main(String[]))
execution(void de.scrum_master.app.Application.bar())
execution(void de.scrum_master.app.Application.baz())
This may help you
execution(* my.package.*.*(..)) && !execution(@annotation * my.package.*.*(..))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With