利用Spring的拦截器监测每个Controller或方法的执行时长

  • Post author:
  • Post category:其他


首先写一个类(TestInterceptor),让他继承HandlerInterceptorAdapter,并重写其中的三个方法,例如:

package com.wechat.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lenovo on 2017/3/14.
 */
public class TestInterceptor extends HandlerInterceptorAdapter {

    private long time;
    private int openingTime;
    private int closingTime;
    private String mappingURL;//利用正则映射到需要拦截的路径
    public void setOpeningTime(int openingTime) {
        this.openingTime = openingTime;
    }
    public void setClosingTime(int closingTime) {
        this.closingTime = closingTime;
    }
    public void setMappingURL(String mappingURL) {
        this.mappingURL = mappingURL;
    }

    /**
     * 1.进入Controller方法之前执行
     * @param request
     * @param response
     * @param handler
     * @return 返回值为true时可继续执行Controller,preHandle()和afterCompletion(),为false时停止执行任何方法。
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        time = System.currentTimeMillis();
        String url=request.getRequestURL().toString();
        if(mappingURL==null || url.matches(mappingURL)){
            Calendar c=Calendar.getInstance();
            c.setTime(new Date());
            int now=c.get(Calendar.HOUR_OF_DAY);
            if(now<openingTime || now>closingTime){
                request.setAttribute("msg", "注册开放时间:" + openingTime + ":00-" + closingTime + ":00");
//                request.getRequestDispatcher("/msg.jsp").forward(request, response);
                return false;
            }
            return true;
        }
        return true;
    }

    /**
     * 2.Controller方法执行完毕但是未进行视图渲染时执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 3.视图渲染完成(整个请求结束)执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("请求处理完成啦!用时:"+(System.currentTimeMillis() - time)+"ms");
        super.afterCompletion(request, response, handler, ex);
    }
}

接下来是spring配置文件:

<!--拦截器-->
<mvc:interceptors>
   <mvc:interceptor>
      <!--配置哪些请求会拦截-->
      <mvc:mapping path="/**"/>
      <bean class="com.wechat.test.TestInterceptor">
         <!--为这个类中的参数写入值-->
         <property name="openingTime" value="16" />
         <property name="closingTime" value="18" />
         <property name="mappingURL" value=".*/index" />
      </bean>
   </mvc:interceptor>
</mvc:interceptors>



版权声明:本文为qq_34742298原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。