Spring mvc 方法调用时间统计

1.javax.servlet.Filter

package com.faunjoe.web.filter;

import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;

/**
 * @author faunjoe 
 * @version 创建时间:2018/5/6 23:10
 */
@Component
public class TimeFilter implements Filter {
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("time filter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("time filter start");
        long start = System.currentTimeMillis();
        chain.doFilter(request, response);
        System.out.println("time filter 耗时:"+ (System.currentTimeMillis() - start));
        System.out.println("time filter finish");
    }

    @Override
    public void destroy() {
        System.out.println("time filter destroy.");
    }
}
@RequestMapping(value = "/getInfo/{id:\\d+}", method = RequestMethod.GET)
    public User getInfo(@PathVariable String id) {
        System.out.println("进入getInfo服务");
        User user = new User();
        user.setId("1");
        user.setPassword("11");
        user.setUsername("111");
        return user;
    }
运行结果
time filter start
进入getInfo服务
time filter 耗时:40
time filter finish

2.FilterRegistrationBean

package com.faunjoe.web.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author faunjoe 
 * @version 创建时间:2018/5/6 23:10
 */
public class TimeFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("time filter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("time filter start");
        long start = System.currentTimeMillis();
        chain.doFilter(request, response);
        System.out.println("time filter 耗时:"+ (System.currentTimeMillis() - start));
        System.out.println("time filter finish");
    }

    @Override
    public void destroy() {
        System.out.println("time filter destroy.");
    }
}
package com.faunjoe.web.config;

import java.util.ArrayList;
import java.util.List;

import com.faunjoe.web.filter.TimeFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author faunjoe E-mail:[email protected]
 * @version 创建时间:2018/5/6 23:10
 */
@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean timeFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        TimeFilter timeFilter = new TimeFilter();
        registrationBean.setFilter(timeFilter);

        List urls = new ArrayList<>();
        urls.add("/*");
        registrationBean.setUrlPatterns(urls);

        return registrationBean;
    }
}
@RequestMapping(value = "/getInfo/{id:\\d+}", method = RequestMethod.GET)
    public User getInfo(@PathVariable String id) {
        System.out.println("进入getInfo服务");
        User user = new User();
        user.setId("1");
        user.setPassword("11");
        user.setUsername("111");
        return user;
    }
运行结果
time filter start
进入getInfo服务
time filter 耗时:40
time filter finish

3.拦截器

package com.faunjoe.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author faunjoe 
 * @version 创建时间:2018/5/6 23:10
 */
@Component
public class TimeInterceptor implements HandlerInterceptor {
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("preHandle");
        System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
        System.out.println(((HandlerMethod) handler).getMethod().getName());
        request.setAttribute("startTime", System.currentTimeMillis());
        return true;
    }
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
        Long start = (Long) request.getAttribute("startTime");
        System.out.println("time interceptor 耗时:" + (System.currentTimeMillis() - start));
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("afterCompletion");
        Long start = (Long) request.getAttribute("startTime");
        System.out.println("time interceptor 耗时:" + (System.currentTimeMillis() - start));
        System.out.println("ex is " + ex);
    }
}
package com.faunjoe.web.config;

import com.faunjoe.web.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author faunjoe E-mail:[email protected]
 * @version 创建时间:2018/5/6 23:10
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @SuppressWarnings("unused")
    @Autowired
    private TimeInterceptor timeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(timeInterceptor);
    }
}
preHandle
com.faunjoe.web.controller.UserController
getInfo
进入getInfo服务
postHandle
time interceptor 耗时:25
afterCompletion
time interceptor 耗时:25
ex is null

你可能感兴趣的:(Spring mvc 方法调用时间统计)