posts - 64,comments - 22,trackbacks - 0

          SpringMVC的攔截器不同于Spring的攔截器,SpringMVC具有統一的入口DispatcherServlet,所有的請求都通過DispatcherServlet,所以只需要在DispatcherServlet上做文章即可,DispatcherServlet也沒有代理,同時SpringMVC管理的Controller也不有代理。

          AD:

          一、Servlet Filter與Spring interceptor的執行順序

          Filter有順序嗎?我們怎么控制filter的執行順序。通過Tomcat的代碼分析,servlet在Filter執行完成后才調用,如有多個filter怎么控制執行順序,首先會想到在web.xml配置某個參數,例如order之類的,但查找一下一番,servlet并沒有這個參數。試試filter Mapping的配置的先后順序,果然有效,原來filter的執行順序就考filter mapping在web.xml中的順序。

          spring interceptor也是這樣的執行順序,不過interceptor多一個配置參數order通過他也可以來實現interceptor的執行順序。很多應用場景中,執行順序還是重要的,比如cache和transaction interceptor的執行順序,很顯然cache應該在transaction之前,這樣發現命中了就不用打開事務,如果transaction在前,每次都打開事務即使cache命中,這是一個無謂東動作。

          二、利用springMVC的interceptor實現頁面性能監控(Filter亦可)

          調優第一步,找出耗時比較長的頁面進行優化。利用interceptor能輕易搞定。interceptor提供了preHandle和postHandle以及afterCompletion三個方法。preHandle調用controller具體方法之前調用,postHandle完成具體方法之后調用,afterCompletion完成對頁面的render以后調用,至此整個頁面渲染完成。也就是說我們在preHandle記錄開始的時間,在afterCompletion記錄結束的時間,就可或者整個頁面生成的時間。Spring自帶StopWatch工具類來實現時間跟蹤,關鍵一點interceptor不是線程安全的。我們需要借助threadlocal來實現線程安全。

          1. @Override 
          2.     public boolean preHandle(HttpServletRequest request, 
          3.             HttpServletResponse response, Object handler) throws Exception { 
          4.         if(usePerformance){ 
          5.             StopWatch stopWatch = new StopWatch(handler.toString()); 
          6.             stopWatchLocal.set(stopWatch); 
          7.             stopWatch.start(handler.toString()); 
          8.         } 
          9.          
          10.         return true
          11.     } 
          12.  
          13.  @Override 
          14.     public void afterCompletion(HttpServletRequest request, 
          15.             HttpServletResponse response, Object handler, Exception ex) 
          16.             throws Exception { 
          17.         if(usePerformance){ 
          18.             StopWatch stopWatch = stopWatchLocal.get(); 
          19.             stopWatch.stop(); 
          20.             String currentPath = request.getRequestURI(); 
          21.             String queryString  = request.getQueryString(); 
          22.             queryString = queryString == null ? "":"?" + queryString; 
          23.             log.info("access url path:" + currentPath + queryString +  " |time:" + stopWatch.getTotalTimeMillis()); 
          24.             stopWatchLocal.set(null); 
          25.         } 
          26.     } 

          如果你沒有使用springMVC可以使用filter來完成:

          1. stopWatch.start(); 
          2. doFilterChain(); 
          3. stopWatch.stop(); 

          三、SpringMVC 攔截器實現分析

          SpringMVC的攔截器不同于Spring的攔截器,SpringMVC具有統一的入口DispatcherServlet,所有的請求都通過DispatcherServlet,所以只需要在DispatcherServlet上做文章即可,DispatcherServlet也沒有代理,同時SpringMVC管理的Controller也不有代理。哪不難想到我們在執行controller之前做某些動作,執行完畢做某些動作,render完成做某些動作。SpringMVC的攔截器對應提供了三個preHandle,postHandle,afterCompletion方法。只需在三個方法內寫我們需要的邏輯就行,多了都是廢話,還是代碼實在。

          1. HandlerInterceptor[] interceptors = mappedHandler.getInterceptors(); 
          2.                 if (interceptors != null) { 
          3.                     for (int i = 0; i < interceptors.length; i++) { 
          4.                         HandlerInterceptor interceptor = interceptors[i]; 
          5. //ha.handle是調用具體的controller在此之前執行preHandle                      if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) { 
          6.                             triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null); 
          7.                             return
          8.                         } 
          9.                         interceptorIndex = i; 
          10.                     } 
          11.                 } 
          12.  
          13.                 // Actually invoke the handler. 
          14.                 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); 

          完成調用之后,調用render(),最后執行afterCompletion()。

          1. if (interceptors != null) { 
          2.                 for (int i = interceptors.length - 1; i >= 0; i--) { 
          3.                     HandlerInterceptor interceptor = interceptors[i]; 
          4.                     interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv); 
          5.                 } 
          6.             } 
          7.         } 
          8.         catch (ModelAndViewDefiningException ex) { 
          9.             logger.debug("ModelAndViewDefiningException encountered", ex); 
          10.             mv = ex.getModelAndView(); 
          11.         } 
          12.         catch (Exception ex) { 
          13.             Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null); 
          14.             mv = processHandlerException(processedRequest, response, handler, ex); 
          15.             errorView = (mv != null); 
          16.         } 
          17.  
          18.         // Did the handler return a view to render? 
          19.         if (mv != null && !mv.wasCleared()) { 
          20.             render(mv, processedRequest, response); 
          21.             if (errorView) { 
          22.                 WebUtils.clearErrorRequestAttributes(request); 
          23.             } 
          24.         } 
          25.         else { 
          26.             if (logger.isDebugEnabled()) { 
          27.                 logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() + 
          28.                         "': assuming HandlerAdapter completed request handling"); 
          29.             } 
          30.         } 
          31.  
          32.         // Trigger after-completion for successful outcome. 
          33.         triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null); 

            原文鏈接:http://exceptioneye.iteye.com/blog/1267248
          posted on 2011-11-22 20:25 hellxoul 閱讀(2379) 評論(0)  編輯  收藏 所屬分類: Spring2.5
          主站蜘蛛池模板: 桓仁| 鞍山市| 盘山县| 阆中市| 平陆县| 宝鸡市| 云阳县| 公安县| 抚州市| 拜城县| 青田县| 临朐县| 河池市| 南郑县| 越西县| 珠海市| 平顶山市| 顺平县| 汽车| 日土县| 金堂县| 方城县| 长顺县| 郓城县| 仙游县| 沙田区| 凤庆县| 珠海市| 家居| 简阳市| 永济市| 托里县| 景德镇市| 锡林郭勒盟| 永修县| 雷波县| 阜城县| 泉州市| 衡东县| 宣威市| 当雄县|