Tag Archives: aspectj

Java, custom annotation, AspectJ, Maven: minimalistic example

Goal: some code must be executed before/after the method annotated with custom annotation.
We need a custom annotation first:

package yourpackage;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public @interface MyCustomAnnotation {
    public boolean isRun() default true;

An aspect:

package yourpackage;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
public class MyCustomAspect {
    @Before("execution(* *.*(..)) && @annotation(MyCustomAnnotation)")
    public void advice(JoinPoint joinPoint) {
        System.out.printf("BINGO! advice() called before '%s'%n", joinPoint);

Continue reading