springboot 框架計算每個方法執行時間,顯示在日誌中
阿新 • • 發佈:2018-12-27
加入aop的jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
//程式碼如下
@Aspect
@Component
public class ServiceAspect {
Logger logger = LoggerFactory.getLogger(ServiceAspect.class);
@Pointcut("execution(* com.service.*.*.*ServiceImpl.*(..))")
private void pointCutMethodService(){
}
@Around("pointCutMethodService()")
public Object doAroundService(ProceedingJoinPoint pjp) throws Throwable{
long begin = System.nanoTime();
Object obj=pjp.proceed();
long end =System.nanoTime();
logger.info("呼叫Service方法:{},引數:{},執行耗時:{}納秒,耗時:{}毫秒",
pjp.getSignature().toString(),Arrays.toString(pjp.getArgs()),(end-begin),(end-begin)/1000000);
return obj;
}
}