spring项目如何打印每个方法的执行时间,比如查询、插入、删除。

1.新建代码拦截器

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author fangtan
 * @ClassName PerformanceInstrumentInterceptor
 * @Description 方法时间拦截器,每个方法阈值为:2秒。
 * @date 2019/8/26 11:32
 * @Modified by:
 */
public class PerformanceInstrumentInterceptor implements MethodInterceptor{

    private static final Logger logger = LoggerFactory
            .getLogger(PerformanceInstrumentInterceptor.class);

    private long threshold=10;

    public long getThreshold() {
        return threshold;
    }

    public void setThreshold(long threshold) {
        this.threshold = threshold;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {

        String name = invocation.getMethod().getDeclaringClass().getName()
                + "." + invocation.getMethod().getName();
//
//        Method method = PerformanceInstrumentInterceptor.class.getDeclaredMethod(name);
//        Parameter[] parameters = method.getParameters();
//        for (Parameter parameter : parameters) {
//            // java.lang.String name
//            // java.lang.Integer age
//            logger.info("对象参数:{}",parameter);
//        }

        long start = System.currentTimeMillis();

        try {
            return invocation.proceed();

        } finally {

            long elapseTime = System.currentTimeMillis() - start;

            if (elapseTime > threshold) {
                StringBuilder builder = new StringBuilder();
                builder.append("方法").append(name);
                builder.append("的执行时间超过阈值").append(threshold).append("毫秒,");
                builder.append("实际执行时间为").append(elapseTime).append("毫秒.\r\n");
                logger.info(builder.toString());
            }
        }

    }
}

2.在配置文件里配置

<bean id="beanNameAutoProxyCreator"
          class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <value>performanceInterceptor</value>
            </list>
        </property>
        <property name="beanNames">
            <list>
                <value>*QueryService</value>
                <value>*Controller</value>
                <value>*QueryService.findByExample</value>
            </list>

        </property>
    </bean>

    <bean id="performanceInterceptor" class="com.zjsft.openapi.web.interceptor.PerformanceInstrumentInterceptor">
        <property name="threshold" value="2000"/>
    </bean>

3.注入到spring里

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private BeanNameAutoProxyCreator beanNameAutoProxyCreator;
}

4.打印记录
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值