天天看点

Spring AOP 统计函数方法执行时间实例

package com.packtpub.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 检测方法执行耗时的spring切面类
 * 使用@Aspect注解的类,Spring将会把它当作一个特殊的Bean(一个切面),也就是不对这个类本身进行动态代理
 *
 * @date 2019-03-29
 */
@Aspect
@Component
public class MethodExecuteTimeViewer {

    // service层的统计耗时切面,类型必须为final String类型的,注解里要使用的变量只能是静态常量类型的
    public static final String POINT = "execution (* com.packtpub.*.*.*(..))";
    private static final long TWO_MILLISECONDS = 2;
    private static Log logger = LogFactory.getLog(MethodExecuteTimeViewer.class);

    /**
     * 统计方法执行耗时Around环绕通知
     *
     * @param joinPoint
     * @return
     */
    @Around(POINT)
    //等价于@Around("execution (* com.packtpub.restapp.*.*(..))";)
    public Object timeAround(ProceedingJoinPoint joinPoint) {
        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();
        long startTime = System.currentTimeMillis();

        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            logger.error("统计某方法执行耗时环绕通知出错", e);
        }

        // 获取执行的方法名
        long endTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

        // 打印耗时的信息
        this.printExecTime(methodName, startTime, endTime);

        return obj;
    }

    /**
     * 打印方法执行耗时的信息,如果超过了一定的时间,才打印
     *
     * @param methodName
     * @param startTime
     * @param endTime
     */
    private void printExecTime(String methodName, long startTime, long endTime) {
        long diffTime = endTime - startTime;
        if (diffTime > TWO_MILLISECONDS) {
            logger.info("-----" + methodName + " 方法执行耗时:" + diffTime + " ms");
        }
    }

}
           

执行

curl http://localhost:8080/user/100

或打开网页 http://http?/localhost:8080/user/100

调用相关的方法,输出了相应的执行时间.

Spring AOP 统计函数方法执行时间实例

https://github.com/HappyFreeAngel/Spring-AOP-MethodExecuteTimeViewer.git