我不熟悉Spring AOP,并通过阅读baeldung文章尝试了以下代码。
方面类:
@Aspect
@Component
public class LoggingAspect {
private final static Logger LOGGER = LoggerFactory.getLogger(LoggingAspect.class);
@Around("@annotation(Debug)")
public Object beforeDebug(ProceedingJoinPoint debugJoinpoint) {
LOGGER.debug("----------------Debug message logged from {}", debugJoinpoint.getSignature().toString());
System.out.println("IN HERE");
long start = System.currentTimeMillis();
Object proceed = null;
try {
proceed = debugJoinpoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
long executionTime = System.currentTimeMillis() - start;
System.out.println(debugJoinpoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
注释:
@Retention(RUNTIME)
@Target(METHOD)
@Loggable(type = "debug")
public @interface Debug {
}
用于测试的演示类:
@SpringBootApplication
@Component
public class Application {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
dumb();
}
@Debug
public static void dumb() throws InterruptedException {
Thread.sleep(2000);
}
}
有人可以指出我上面做错了什么吗?该建议将无法执行,并且在四处搜寻之后我无法解决此问题,不确定我丢失了什么。
答案 0 :(得分:0)
Spring AOP仅适用于非静态方法,如Baeldung示例中所示,并记录在Spring手册中。