我已收到监视所有数据访问层方法的性能的请求。自然,即使我有0点经验,AOP也是我想到的第一件事。
进行了平时的研究,发现有许多实现此目标的实现。我倾向于简单的。
我根据自己的需要缝制了一条建议,这就是我想出的。
对于这个项目,我正在使用Spring Boot v1.5.10.RELEASE
。我尝试监视的DAOImpl中的方法返回了POJO。
@Aspect
@Component
public class PerformanceLoggingAspect {
@Pointcut("execution(* com.xyz.quote.dao.*.*(..))")
public void publicDaoMethods() {
}
@Around("publicDaoMethods()")
public Object aroundPublicDaoMethods(ProceedingJoinPoint point) throws Throwable {
Instant before = Instant.now();
Object toReturn = null;
try {
toReturn = point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
Instant after = Instant.now();
System.out.println(point.getSignature()+" exec time: "+Duration.between(before,after).toMillis()+"ms");
return toReturn;
}
}
上面的代码似乎运行良好(根据手动测试+对数据的集成测试)。但是它看起来并不“自然”,感觉太hacky。 我是否缺少某些东西,或者这是实现性能日志记录的正确方法之一?