springboot 框架计算每个方法执行时间,显示在日志中

加入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;

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_41126842/article/details/84334429