SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo

使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中.

RequestMappingInfo封装了PatternsRequestCondition,RequestMethodsRequestCondition,ParamsRequestCondition等,所以自己不干活,所有的活都是委托给具体的condition处理.

 

先看下封装的RequestCondition吧,之前的文章将的比较细了,不清楚各个类具体是做什么的,可以移步这里<SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系>

 1 package org.springframework.web.servlet.mvc.method;
 2 public final class RequestMappingInfo implements RequestCondition<RequestMappingInfo> {
 3 
 4     private final PatternsRequestCondition patternsCondition;
 5 
 6     private final RequestMethodsRequestCondition methodsCondition;
 7 
 8     private final ParamsRequestCondition paramsCondition;
 9 
10     private final HeadersRequestCondition headersCondition;
11 
12     private final ConsumesRequestCondition consumesCondition;
13 
14     private final ProducesRequestCondition producesCondition;
15 
16     private final RequestConditionHolder customConditionHolder;
17 }

 

初始化没什么好看的,直接看接口的实现吧.

貌似combine也没啥料,就是分别委托

 1     /**
 2      * Combines "this" request mapping info (i.e. the current instance) with another request mapping info instance.
 3      * <p>Example: combine type- and method-level request mappings.
 4      * @return a new request mapping info instance; never {@code null}
 5      */
 6     public RequestMappingInfo combine(RequestMappingInfo other) {
 7         PatternsRequestCondition patterns = this.patternsCondition.combine(other.patternsCondition);
 8         RequestMethodsRequestCondition methods = this.methodsCondition.combine(other.methodsCondition);
 9         ParamsRequestCondition params = this.paramsCondition.combine(other.paramsCondition);
10         HeadersRequestCondition headers = this.headersCondition.combine(other.headersCondition);
11         ConsumesRequestCondition consumes = this.consumesCondition.combine(other.consumesCondition);
12         ProducesRequestCondition produces = this.producesCondition.combine(other.producesCondition);
13         RequestConditionHolder custom = this.customConditionHolder.combine(other.customConditionHolder);
14 
15         return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom.getCondition());
16     }

 

getMatchingCondition只是体现出可能基于性能消耗的考虑,把PatternsRequestCondition和RequestConditionHolder的比较放到后面单独处理了.

 1     /**
 2      * Checks if all conditions in this request mapping info match the provided request and returns
 3      * a potentially new request mapping info with conditions tailored to the current request.
 4      * <p>For example the returned instance may contain the subset of URL patterns that match to
 5      * the current request, sorted with best matching patterns on top.
 6      * @return a new instance in case all conditions match; or {@code null} otherwise
 7      */
 8     public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {
 9         RequestMethodsRequestCondition methods = methodsCondition.getMatchingCondition(request);
10         ParamsRequestCondition params = paramsCondition.getMatchingCondition(request);
11         HeadersRequestCondition headers = headersCondition.getMatchingCondition(request);
12         ConsumesRequestCondition consumes = consumesCondition.getMatchingCondition(request);
13         ProducesRequestCondition produces = producesCondition.getMatchingCondition(request);
14 
15         if (methods == null || params == null || headers == null || consumes == null || produces == null) {
16             return null;
17         }
18 
19         PatternsRequestCondition patterns = patternsCondition.getMatchingCondition(request);
20         if (patterns == null) {
21             return null;
22         }
23 
24         RequestConditionHolder custom = customConditionHolder.getMatchingCondition(request);
25         if (custom == null) {
26             return null;
27         }
28 
29         return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, custom.getCondition());
30     }

 

compareTo就是排了个不同RequestCondition的优先级

 1     /**
 2      * Compares "this" info (i.e. the current instance) with another info in the context of a request.
 3      * <p>Note: it is assumed both instances have been obtained via
 4      * {@link #getMatchingCondition(HttpServletRequest)} to ensure they have conditions with
 5      * content relevant to current request.
 6      */
 7     public int compareTo(RequestMappingInfo other, HttpServletRequest request) {
 8         int result = patternsCondition.compareTo(other.getPatternsCondition(), request);
 9         if (result != 0) {
10             return result;
11         }
12         result = paramsCondition.compareTo(other.getParamsCondition(), request);
13         if (result != 0) {
14             return result;
15         }
16         result = headersCondition.compareTo(other.getHeadersCondition(), request);
17         if (result != 0) {
18             return result;
19         }
20         result = consumesCondition.compareTo(other.getConsumesCondition(), request);
21         if (result != 0) {
22             return result;
23         }
24         result = producesCondition.compareTo(other.getProducesCondition(), request);
25         if (result != 0) {
26             return result;
27         }
28         result = methodsCondition.compareTo(other.getMethodsCondition(), request);
29         if (result != 0) {
30             return result;
31         }
32         result = customConditionHolder.compareTo(other.customConditionHolder, request);
33         if (result != 0) {
34             return result;
35         }
36         return 0;
37     }

 

覆写的equals,hashCode,toString不看也罢

 

posted @ 2016-02-24 15:14  出门向左  阅读(6618)  评论(0编辑  收藏  举报