Search code examples
javaaopaspectjpointcutjava-annotations

AOP not invoked by annotation


I am trying to invoke a AOP method for every method that is annotated with @Timed.

My AOP MyTracer.java

@Aspect
public class MyTracer {

  private static final Tracer tracer = Tracing.getTracer();
  private static final Logger log = Logger.getLogger(MyTracer.class.getName());

  @Around("@annotation(timed)")
  public Object trackExecutionTime(ProceedingJoinPoint proceedingJoinPoint, Timed timed) throws Throwable {
    String methodName = proceedingJoinPoint.getSignature().getName();
    log.info("Using AOP for method name" + methodName); 
    log.info("Using AOP for param name" + timed.label()); 
    return output;
    }
  }
}

My Annotation

Timed.java

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Timed {
  String label() default "label";
}

Finally my business code

HelloWorld.java

  @Override
  @Timed(label = "myMetrics")
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    log.info("Servlet called");
    PrintWriter out = resp.getWriter();
    out.println("Hello, world - Flex Servlet");
  }

At runtime, I am getting logs from the servlets but not the MyTracer class.


Solution

  • Ok, I figured it out. The build.gradle was missing the following. I replaced

    compile group: 'org.aspectj', name: 'aspectjtools', version: '1.6.2' 
    

    with

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
      }
    }
    
    project.ext {
      aspectjVersion = "1.8.2"
    }
    
    apply plugin: "aspectj.gradle"