AOP切面测方法耗时

<bean id="timeAspect" class="com.demo.TimeCount"/>
<aop:config>
    <aop:pointcut id="allPointcut" expression="execution (* com.demo..*(..))"/>
    <aop:aspect order="99" ref="timeAspect">
        <aop:around method="aroundMethod" pointcut-ref="allPointcut"/>
    </aop:aspect>
</aop:config>
package com.demo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//@Aspect
//@Component
public class TimeCount {
    private static Logger logger = LoggerFactory.getLogger(TimeCount.class);
    private static long baseTime = System.currentTimeMillis();


    public Object aroundAopMethod(ProceedingJoinPoint joinPoint) {
        // 获取执行的方法名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();

        long startTime = System.currentTimeMillis() - baseTime;
        System.out.println(methodName + " aop start at " + startTime);

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

        long endTime = System.currentTimeMillis() - baseTime;
        long diffTime = endTime - startTime;
        System.out.println(methodName + " aop finish at " + endTime + ", duration " + diffTime + "ms");

        return obj;
    }

    /**
     * 统计方法执行耗时Around环绕通知
     */
//    @Around("execution (* com.demo..*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        // 获取执行的方法名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();

        long startTime = System.currentTimeMillis() - baseTime;
        System.out.println(methodName + " start at " + startTime);

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

        long endTime = System.currentTimeMillis() - baseTime;
        long diffTime = endTime - startTime;
        System.out.println(methodName + " finish at " + endTime + ", duration " + diffTime + "ms");

        return obj;
    }

    /**
     * 打印方法执行耗时的信息,如果超过了一定的时间,才打印
     */
    private void printExecTime(String methodName, long startTime, long endTime) {
    }
}
使用Spring AOP来统计方法的执行时间

Autodesk 123D Design 下载地址

https://labs-download.autodesk.com/us/labs/trials/worldwide/123D_Design_R2.2_WIN64_2.2.14.exe
https://labs-download.autodesk.com/us/labs/trials/worldwide/123DCatch.msi
https://labs-download.autodesk.com/us/labs/trials/worldwide/Autodesk_123D_Make_1.6.0_WIN.exe

https://download.freedownloadmanager.org/Windows-PC/123D-Design/FREE-2.2.14.html
https://download.freedownloadmanager.org/Windows-PC/Autodesk-123D-Catch/FREE-3.0.0.90.html
https://download.freedownloadmanager.org/Windows-PC/Autodesk-123D-Make/FREE-1.60.html

最新文章

最近回复

  • muumlover: 你好
  • 测试人: 你好呀
  • ww: 看了这么多就这个有用,牛逼
  • 未知狐: 啊确实,话说,博主博客好像没有评论回复邮件提醒,可以试试Comm...
  • Skogen: 系统的差异,不同的Linux发行版集成的包也不一样,安装难度也就...
  • Skogen: 系统是 Debian9,理论上Debian系的系统应该通用的。
  • 2247: 请问用的是什么系统?
  • 未知狐: 受教了,我比博主运气好点,是因为不理解命令参数等设定而频繁失败,...
  • Skogen: 23
  • Skogen: asd

分类

标签

归档

其它