springMVC源码分析--拦截器HandlerExecutionChain(三)

上一篇博客springMVC源码分析--HandlerInterceptor拦截器调用过程(二)中我们介绍了HandlerInterceptor的执行调用地方,最终HandlerInterceptor调用的地方是在HandlerExecutionChain中,接下来我们就介绍一下HandlerExecutionChain。HandlerExecutionChain有3个变量:

Object handler;不做过多介绍,存储的对象是HandlerMethod

HandlerInterceptor[] interceptors :所有的HandlerInterceptor的数组

List<HandlerInterceptor> interceptorList:所有的HandlerInterceptor的链表

之前的博客中我们已经介绍了HandlerExecutionChain中执行HandlerInterceptor方法的函数

执行HandlerInterceptor的preHandler方法

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Apply preHandle methods of registered interceptors. 
  3.      * @return {@code true} if the execution chain should proceed with the 
  4.      * next interceptor or the handler itself. Else, DispatcherServlet assumes 
  5.      * that this interceptor has already dealt with the response itself. 
  6.      */  
  7.     //依次执行HandlerInterceptor实现类的preHandle函数  
  8.     boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  9.         HandlerInterceptor[] interceptors = getInterceptors();  
  10.         if (!ObjectUtils.isEmpty(interceptors)) {  
  11.             for (int i = 0; i < interceptors.length; i++) {  
  12.                 HandlerInterceptor interceptor = interceptors[i];  
  13.                 if (!interceptor.preHandle(request, response, this.handler)) {  
  14.                     triggerAfterCompletion(request, response, null);  
  15.                     return false;  
  16.                 }  
  17.                 this.interceptorIndex = i;  
  18.             }  
  19.         }  
  20.         return true;  
  21.     }  
执行HandlerInterceptor实现类的postHandle函数

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //依次执行HandlerInterceptor实现类的postHandle函数  
  2.     void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {  
  3.         HandlerInterceptor[] interceptors = getInterceptors();  
  4.         if (!ObjectUtils.isEmpty(interceptors)) {  
  5.             for (int i = interceptors.length - 1; i >= 0; i--) {  
  6.                 HandlerInterceptor interceptor = interceptors[i];  
  7.                 interceptor.postHandle(request, response, this.handler, mv);  
  8.             }  
  9.         }  
  10.     }  
执行HandlerInterceptor实现类的afterCompletion函数
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //依次执行HandlerInterceptor实现类的postHandle函数  
  2.     void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {  
  3.         HandlerInterceptor[] interceptors = getInterceptors();  
  4.         if (!ObjectUtils.isEmpty(interceptors)) {  
  5.             for (int i = interceptors.length - 1; i >= 0; i--) {  
  6.                 HandlerInterceptor interceptor = interceptors[i];  
  7.                 interceptor.postHandle(request, response, this.handler, mv);  
  8.             }  
  9.         }  
  10.     }  
执行HandlerInterceptor实现类的afterCompletion函数
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //依次执行HandlerInterceptor实现类的afterCompletion函数  
  2.     void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)  
  3.             throws Exception {  
  4.   
  5.         HandlerInterceptor[] interceptors = getInterceptors();  
  6.         if (!ObjectUtils.isEmpty(interceptors)) {  
  7.             for (int i = this.interceptorIndex; i >= 0; i--) {  
  8.                 HandlerInterceptor interceptor = interceptors[i];  
  9.                 try {  
  10.                     interceptor.afterCompletion(request, response, this.handler, ex);  
  11.                 }  
  12.                 catch (Throwable ex2) {  
  13.                     logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);  
  14.                 }  
  15.             }  
  16.         }  
  17.     }  
完整的HandlerExecutionChain的源码,主要就是以上几个函数的操作

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class HandlerExecutionChain {  
  2.   
  3.     private static final Log logger = LogFactory.getLog(HandlerExecutionChain.class);  
  4.   
  5.     private final Object handler;  
  6.   
  7.     private HandlerInterceptor[] interceptors;  
  8.   
  9.     private List<HandlerInterceptor> interceptorList;  
  10.   
  11.     private int interceptorIndex = -1;  
  12.   
  13.     public HandlerExecutionChain(Object handler) {  
  14.         this(handler, (HandlerInterceptor[]) null);  
  15.     }  
  16.   
  17.     public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {  
  18.         if (handler instanceof HandlerExecutionChain) {  
  19.             HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;  
  20.             this.handler = originalChain.getHandler();  
  21.             this.interceptorList = new ArrayList<HandlerInterceptor>();  
  22.             CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);  
  23.             CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);  
  24.         }  
  25.         else {  
  26.             this.handler = handler;  
  27.             this.interceptors = interceptors;  
  28.         }  
  29.     }  
  30.   
  31.     public Object getHandler() {  
  32.         return this.handler;  
  33.     }  
  34.   
  35.     public void addInterceptor(HandlerInterceptor interceptor) {  
  36.         initInterceptorList().add(interceptor);  
  37.     }  
  38.   
  39.     public void addInterceptors(HandlerInterceptor... interceptors) {  
  40.         if (!ObjectUtils.isEmpty(interceptors)) {  
  41.             initInterceptorList().addAll(Arrays.asList(interceptors));  
  42.         }  
  43.     }  
  44.   
  45.     private List<HandlerInterceptor> initInterceptorList() {  
  46.         if (this.interceptorList == null) {  
  47.             this.interceptorList = new ArrayList<HandlerInterceptor>();  
  48.             if (this.interceptors != null) {  
  49.                 // An interceptor array specified through the constructor  
  50.                 this.interceptorList.addAll(Arrays.asList(this.interceptors));  
  51.             }  
  52.         }  
  53.         this.interceptors = null;  
  54.         return this.interceptorList;  
  55.     }  
  56.   
  57.     /** 
  58.      * Return the array of interceptors to apply (in the given order). 
  59.      * @return the array of HandlerInterceptors instances (may be {@code null}) 
  60.      */  
  61.     public HandlerInterceptor[] getInterceptors() {  
  62.         if (this.interceptors == null && this.interceptorList != null) {  
  63.             this.interceptors = this.interceptorList.toArray(new HandlerInterceptor[this.interceptorList.size()]);  
  64.         }  
  65.         return this.interceptors;  
  66.     }  
  67.   
  68.   
  69.     /** 
  70.      * Apply preHandle methods of registered interceptors. 
  71.      * @return {@code true} if the execution chain should proceed with the 
  72.      * next interceptor or the handler itself. Else, DispatcherServlet assumes 
  73.      * that this interceptor has already dealt with the response itself. 
  74.      */  
  75.     //依次执行HandlerInterceptor实现类的preHandle函数  
  76.     boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  77.         HandlerInterceptor[] interceptors = getInterceptors();  
  78.         if (!ObjectUtils.isEmpty(interceptors)) {  
  79.             for (int i = 0; i < interceptors.length; i++) {  
  80.                 HandlerInterceptor interceptor = interceptors[i];  
  81.                 if (!interceptor.preHandle(request, response, this.handler)) {  
  82.                     triggerAfterCompletion(request, response, null);  
  83.                     return false;  
  84.                 }  
  85.                 this.interceptorIndex = i;  
  86.             }  
  87.         }  
  88.         return true;  
  89.     }  
  90.   
  91.     /** 
  92.      * Apply postHandle methods of registered interceptors. 
  93.      */  
  94.     //依次执行HandlerInterceptor实现类的postHandle函数  
  95.     void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {  
  96.         HandlerInterceptor[] interceptors = getInterceptors();  
  97.         if (!ObjectUtils.isEmpty(interceptors)) {  
  98.             for (int i = interceptors.length - 1; i >= 0; i--) {  
  99.                 HandlerInterceptor interceptor = interceptors[i];  
  100.                 interceptor.postHandle(request, response, this.handler, mv);  
  101.             }  
  102.         }  
  103.     }  
  104.   
  105.     //依次执行HandlerInterceptor实现类的afterCompletion函数  
  106.     void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)  
  107.             throws Exception {  
  108.   
  109.         HandlerInterceptor[] interceptors = getInterceptors();  
  110.         if (!ObjectUtils.isEmpty(interceptors)) {  
  111.             for (int i = this.interceptorIndex; i >= 0; i--) {  
  112.                 HandlerInterceptor interceptor = interceptors[i];  
  113.                 try {  
  114.                     interceptor.afterCompletion(request, response, this.handler, ex);  
  115.                 }  
  116.                 catch (Throwable ex2) {  
  117.                     logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);  
  118.                 }  
  119.             }  
  120.         }  
  121.     }  
  122.   
  123.     /** 
  124.      * Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors. 
  125.      */  
  126.     //这个方法会在Controller方法异步执行时开始执行  
  127.     void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response) {  
  128.         HandlerInterceptor[] interceptors = getInterceptors();  
  129.         if (!ObjectUtils.isEmpty(interceptors)) {  
  130.             for (int i = interceptors.length - 1; i >= 0; i--) {  
  131.                 if (interceptors[i] instanceof AsyncHandlerInterceptor) {  
  132.                     try {  
  133.                         AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptors[i];  
  134.                         asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);  
  135.                     }  
  136.                     catch (Throwable ex) {  
  137.                         logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);  
  138.                     }  
  139.                 }  
  140.             }  
  141.         }  
  142.     }  
  143.   
  144.   
  145.     /** 
  146.      * Delegates to the handler's {@code toString()}. 
  147.      */  
  148.     @Override  
  149.     public String toString() {  
  150.         if (this.handler == null) {  
  151.             return "HandlerExecutionChain with no handler";  
  152.         }  
  153.         StringBuilder sb = new StringBuilder();  
  154.         sb.append("HandlerExecutionChain with handler [").append(this.handler).append("]");  
  155.         if (!CollectionUtils.isEmpty(this.interceptorList)) {  
  156.             sb.append(" and ").append(this.interceptorList.size()).append(" interceptor");  
  157.             if (this.interceptorList.size() > 1) {  
  158.                 sb.append("s");  
  159.             }  
  160.         }  
  161.         return sb.toString();  
  162.     }  
  163.   
  164. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值