使用注解的方式实现StopWatch查看程序执行时间(高级篇)

上篇文章说到使用原生的StopWatch查看程序执行时间,本文将介绍使用注解+AOP(面向切面编程)的方式实现其功能,一来可以快速使用StopWatch功能,二来让大家熟悉一下如何使用注解进行切面编程

1. 自定义一个注解 StopWatchTime

@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface StopWatchTime {
    String value() default "";
}复制代码

本注解主要用在方法上,故target为METHOD,并设定一个value,可用于定义此次观察的名称。

2. 创建切面类

  • 引用AOP支持包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>复制代码
  • 创建切面类
@Aspect   // 1
@Component  // 2
public class StopWatchTimeAdvice {

    @Around("@annotation(stopWatchTime)")  // 3
    public Object invoke(ProceedingJoinPoint thisJoinPoint, StopWatchTime stopWatchTime) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
        String name = StringUtils.isEmpty(stopWatchTime.value()) ? methodSignature.getName() : stopWatchTime.value();  // 4

        StopWatch stopWatch = new StopWatch(name);
        stopWatch.start("00");

        Object object = thisJoinPoint.proceed();  // 5

        stopWatch.stop();
        System.err.println(stopWatch.prettyPrint());  // 6

        return object;
    }

}复制代码

代码解释

  1. 通过@Aspect 注解声明一个切面
  2. 通过@Component 让此切面成为Spring容器管理的Bean
  3. 通过@Around注解声明一个建言,并使用定义了自定义注解@StopWatchTime的方法作为切入点
  4. 获取注解上的属性,如果无就默认当前的方法名“ methodSignature.getName() ”
  5. 作好StopWatch的前期准备后,执行切入点的方法体
  6. 执行完成后打印执行结果

3. 注解被观察方法

    @StopWatchTime
    public void run(String... strings) throws Exception {
        step1();
        step2();
        step3();
    }

    private void step1() throws InterruptedException {
        Thread.sleep(100L);
    }

    private void step2() throws InterruptedException {
        Thread.sleep(850L);
    }

    private void step3() throws InterruptedException {
        Thread.sleep(2000L);
    }复制代码

只要@StopWatchTime写在哪个方法上就能监控得出哪个方法的执行时间,但且限于public非静态方法

输出结果

StopWatch 'run': running time (millis) = 2985
-----------------------------------------
ms     %     Task name
-----------------------------------------
02985  100%  00

大家可能会看出来,此方法虽然使用起来方便,但只能显示单个方法的执行时间,下一篇将告诉大家如何使用AOP的方法扩展出可同时观察一个代码段里多个方法的执行情况,敬请期待。

转载于:https://juejin.im/post/59f748c65188254a701e5707

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值