zoukankan      html  css  js  c++  java
  • SpringMVC拦截器-性能监控

    开发步骤:
    1、编写拦截器,记录Controller方法执行时间


    package cn.itcast.jk.interceptor;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.core.NamedThreadLocal;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;


    /**
     * @Description:
     * @Author: nutony
     * @Company: http://java.itcast.cn
     * @CreateDate: 2014-3-6
     */
    public class TimerInterceptor implements HandlerInterceptor {
     private NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("WatchExecuteTime");
     
     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
      long beginTime = System.currentTimeMillis();   //开始时间
      startTimeThreadLocal.set(beginTime);
      return true;
     }
     
     public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {
      long endTime = System.currentTimeMillis();
      long executeTime = endTime - startTimeThreadLocal.get();
      System.out.println(String.format("%s execute %d ms." , req.getRequestURI() , executeTime));
     }
     
     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
       Object arg2, ModelAndView arg3) throws Exception {
     }
    }

    2、在springmvc-servlet.xml(spring-mvc.xml)文件中配置拦截器


    对多个controller进行指定方法的拦截
     <mvc:interceptors>
      <mvc:interceptor>
       <mvc:mapping path="/cargo/export/list.action"/>
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
      <mvc:interceptor>
       <mvc:mapping path="/cargo/packinglist/list.action"/>
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
     </mvc:interceptors>


    对多个controller的所有方法拦截
     <mvc:interceptors>
      <mvc:interceptor>
       <mvc:mapping path="/cargo/export/*"/>
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
      <mvc:interceptor>
       <mvc:mapping path="/cargo/packinglist/*"/>
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
     </mvc:interceptors>


    对某目录下的controller进行拦截

     <mvc:interceptors>
      <mvc:interceptor>
       <mvc:mapping path="/cargo/**"/>
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
     </mvc:interceptors>

     拦截整个项目的所有controller

     <mvc:interceptors>
      <mvc:interceptor>
       <mvc:mapping path="/**"/>

    //TimerInterceptor的路径
       <bean class="cn.itcast.jk.interceptor.TimerInterceptor"/>
      </mvc:interceptor>
     </mvc:interceptors>

  • 相关阅读:
    HibernateTools实现pojo类 数据库schma mapping映射的相互转换
    hibernate基础(1)
    使用Myeclipse完成Hibernate的逆向工程
    hibernate.cfg.xml文件的说明
    hibernate自动生成映射文件
    为什么出现ORM
    hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类
    geotools修改shapefile 属性名乱码问题
    HDU 4584
    HDU 4576 Robot
  • 原文地址:https://www.cnblogs.com/xusongfeng/p/8359567.html
Copyright © 2011-2022 走看看