zoukankan      html  css  js  c++  java
  • 注解 记录方法执行时间


    @GetMapping("/getAllRegion")
    @StopWatchTime
    public ResultBody getAllRegion() {

    return ResultBody.success();
    }




    @Retention(RetentionPolicy.RUNTIME)
    @Target({METHOD})
    public @interface StopWatchTime {
    String value() default "";
    //时间单位 s || ms
    String company() default "ms";
    }


    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.time.StopWatch;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;

    @Aspect
    @Component
    @Slf4j
    public class StopWatchTimeAdvice {

    /*@Pointcut("@annotation(com.shopcc.bmg.compass.aspect.StopWatchTime)")
    public void methodPointcut() {}*/

    //@Around("methodPointcut() && @annotation(stopWatchTime)")
    @Around("@annotation(stopWatchTime)")
    public Object invoke(ProceedingJoinPoint thisJoinPoint, StopWatchTime stopWatchTime) throws Throwable {

    log.info("Annotation company -->{}",stopWatchTime.company());

    //方法名
    String methodName = thisJoinPoint.getSignature().getName();
    //参数
    Object[] args = thisJoinPoint.getArgs();

    StopWatch stopwatch = StopWatch.createStarted();

    //执行目标方法
    Object object = thisJoinPoint.proceed();

    stopwatch.stop();

    log.info("method name :{}({}) execute time :{}:{}",
    methodName,args, stopwatch.getTime(StringUtils.equals(stopWatchTime.company(),"ms") ? TimeUnit.MILLISECONDS : TimeUnit.SECONDS),
    stopWatchTime.company());

    return object;
    }
    }
    能力是有限的,努力是无限的。
  • 相关阅读:
    将截断字符串或二进制数据。语句已终止的解决方法
    201812-1 小明上学 Java
    201809-2 买菜 Java
    201809-1 卖菜 Java
    201803-2 碰撞的小球 Java
    201803-1 跳一跳 Java
    201712-2 游戏 Java
    201712-1 最小差值 Java
    201709-2 公共钥匙盒 Java
    201709-1 打酱油 Java
  • 原文地址:https://www.cnblogs.com/jiahaoJAVA/p/14873928.html
Copyright © 2011-2022 走看看