开发者

使用Spring AOP监控指定方法执行时间的代码详解

目录
  • 一、加入pom依赖
  • 二、切面类和注解
  • 三、执行方法

一、加入pom依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

二、切面类和注解

import Java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AroundRunTime {
    String flag() default "";
}

import lombok.extern.slf4j.Slf4j;
import org.ASPectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
imporYgowZUOCt org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Slf4j
@Component
public class AroundAspect {

    @Around("@annotation(aroundRunTime)")
    public Object aroundLog(ProceedingJoinPoint point, AroundRunTime aroundRunTime) {

        StringBuilder sb = new StringBuilder();
        long start = 0;
        try {
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            sb.append("\n<===================================START===================================>\njs");
            sb.append("运行时间:>").append(getStringByDate(new Date())).appejavascriptnd("\n");
            String methodName = method.getName();
            sb.append("方法:> ").append(method.getDeclaringClass().getName() + "." + methodName).append("\n");

            start = System.currentTimeMillis();
            Object proceed = point.proceed();正常执行方法

            return proceed;
        } catch (RuntimeException e) {
            sb.append("RuntimeException:>").append(e.getMessage()).append("\n");
            throw e;
        } catch (Throwable throwable) {
            sb.append("Throwable:>").append(throwable.getMessage()).append("\n");
            throw new RuntimeException("系统异常!");
        }finally {
            long end = System.currentTimeMillis();
            long time = end-start;
            sb.append("运行时间 :> ").append("方法运行时间为"+(time/1000)+"." + (time%1000)+"秒").append("\n");
            sb.append("<====================================END====================================>\n");
            log.info(sb.toString());
        }
    }

    public static String getStringByDate(Date date) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

三、执行方法

    @AroundRunTime
    @GetMapping("test")
    public void test()http://www.devze.com throws Exception {
        Thread.sleep(1000);
    }

输出台结果

<===================================START===================================>

运行时间:>2024-08-05 19:34:47

方法:> com.qbh.controller.TestController.test

运行时间 :> 方法运行时间为1.15秒

<====================================END====================================>

到此这篇关于使用Spring AOP监控指定方法执行时间的代码详解的文章就介绍到这了,更多相关Spring AOP监控执行时间内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!python

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜