代码如下:
@Aspect
@Slf4j
@Component
public class PerformanceAspect {
// 用于统计一个方法的执行时间和结果(成功执行、执行出错)
@Around("repositoryOps()")
public Object logPerformance(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
String methodName = "-";
String result = "Y";
try {
methodName = pjp.getSignature().toShortString();
return pjp.proceed();
} catch (Throwable t) {
result = "N";
throw t;
} finally {
long end = System.currentTimeMillis();
log.info("{};{};{}ms", methodName, result, (end - start));
}
}
// cn.wu.service 包下的任意类的任意方法
@Pointcut("execution(* cn.wu.service.*.*(..))")
private void repositoryOps() {
}
}
结果如下:
cn.wu.aspect.PerformanceAspect : UserService.findAll();Y;655ms
版权声明:本文为wzm212原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。