我有一个可用的 AOP(在它被写入的项目内部使用时)但是当我构建这个项目(maven 安装)并在另一个项目中使用该 JAR 并尝试使用@TimedLog注释时,没有任何反应。我试图断点到它,但它没有到达那里。
它看起来像这样:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimedLog {
boolean shouldAttachMethodArgs() default false;
boolean shouldAttachReturnValue() default false;
String message() default "";
}
这是实际的方面:
@Aspect
@Configuration
@Slf4j
public class MethodExecutionAspect {
@Pointcut("@annotation(timedLogVar)")
public void annotationPointCutDefinition(TimedLog timedLogVar) {}
@Pointcut("execution(* *(..))")
public void atExecution() {}
@Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
Stopwatch stopwatch = Stopwatch.createStarted();
Object returnValue = joinPoint.proceed();
stopwatch.stop();
log.info(String.format("test message %s", stopwatch.elapsed(TimeUnit.MILLISECONDS)));
return returnValue;
}
}
它的一个实现是:
@TimedLog
void testDefaultValues() throws InterruptedException {
int sleepTimeInMillis = 200;
log.info("Resting for {} millis", value("sleepTimeInMillis", sleepTimeInMillis));
Thread.sleep(sleepTimeInMillis);
}
pom.xml
<!-- AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
<scope>compile</scope>
</dependency>
从这里可以看到,这是一个装饰方法并记录其运行时的 AOP。
我已经为此苦苦挣扎了一段时间,非常感谢您的帮助。
森林海
相关分类