AOP 未被注解调用
AOP not invoked by annotation
我正在尝试为每个用 @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;
}
}
}
我的批注
Timed.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 class。
好的,我明白了。 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"
我正在尝试为每个用 @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;
}
}
}
我的批注
Timed.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 class。
好的,我明白了。 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"