0

我正在尝试为使用 @Timed 注释的每个方法调用 AOP 方法。

我的 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;
    }
  }
}

我的注释

定时.java

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

最后是我的业务代码

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");
  }

在运行时,我从 servlet 但不是 MyTracer 类获取日志。

4

1 回答 1

0

好的,我想通了。build.gradle缺少以下内容。我换了

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

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"
于 2018-05-05T18:22:24.667 回答