前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@PrintRunTime – AOP最佳实践:打印方法执行时间

@PrintRunTime – AOP最佳实践:打印方法执行时间

作者头像
收心
发布2022-12-05 08:41:10
5150
发布2022-12-05 08:41:10
举报
文章被收录于专栏:Java实战博客

本页目录

第一步,添加单位枚举PRTUnit

代码语言:javascript
复制
public enum PRTUnit {
    ms,
    second,
    minute,
    hour,
    day;
}

第二步,编写注解PrintRunTime

代码语言:javascript
复制
@Documented
@Target(ElementType.METHOD) // 作用与方法上
@Retention(RetentionPolicy.RUNTIME) // RUNTIME: 在运行时有效(即运行时保留)
public @interface PrintRunTime {

    @AliasFor("unit") // @AliasFor 表示其可与unit互换别名:当注解指定value时,为unit赋值
    PRTUnit value() default PRTUnit.ms;

    // 定义单个文件最大限制
    @AliasFor("value") // @AliasFor 表示其可与value互换别名:当注解指定unit时,为value赋值
    PRTUnit unit() default PRTUnit.ms;

}

第三步,配置AOP规则

代码语言:javascript
复制
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class PrintRunTimeAop {
    private static final TimeInterval timer = DateUtil.timer();

    // 注意,这里要指定注解的全限定类名。不然无法进入AOP拦截自定义注解PrintRunTime
    @Pointcut("@annotation(com.zanglikun.springdataredisdemo.aop.runtime.PrintRunTime)")
    public void pointcut() {
    }

    /**
     * 方法体执行之前执行
     */
    @Before("pointcut()")
    public void beforeMethadRun(JoinPoint joinPoint) {
        // 使用Hutool的代码运行时间工具
        timer.restart();
    }

    @After("pointcut()")
    public void afterMethedEnd(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        PrintRunTime annotation = AnnotationUtils.getAnnotation(signature.getMethod(), PrintRunTime.class);
        Double showTime = 0.0;
        if (StringUtils.equals("ms", annotation.unit().toString())) {
            showTime += timer.intervalMs();
        } else if (StringUtils.equals("second", annotation.unit().toString())) {
            showTime += timer.intervalSecond();
        } else if (StringUtils.equals("min", annotation.unit().toString())) {
            showTime += timer.intervalMinute();
        } else if (StringUtils.equals("hour", annotation.unit().toString())) {
            showTime += timer.intervalHour();
        } else if (StringUtils.equals("day", annotation.unit().toString())) {
            showTime += timer.intervalDay();
        } else {
            log.error("Unit Exception");
        }
        log.info("方法名:{} 执行了:{} {}", signature.getMethod().getName(), showTime, annotation.unit().toString());
    }

}

第四步,测试

代码语言:javascript
复制
    @RequestMapping("/test1")
    @PrintRunTime(value = PRTUnit.second)
    public String test1() {
        return "redirect:/abc.html";
    }
代码语言:javascript
复制
2022-12-03 19:12:30.019  INFO 40198 --- [nio-8081-exec-6] c.z.s.aop.runtime.PrintRunTimeAop        : 方法名:test1 执行了0.0 second

特殊说明: 以上文章,均是我实际操作,写出来的笔记资料,不会盗用别人文章!烦请各位,请勿直接盗用!转载记得标注来源!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一步,添加单位枚举PRTUnit
  • 第二步,编写注解PrintRunTime
  • 第三步,配置AOP规则
  • 第四步,测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档