springmvc中的requestmapping注解怎么实现的

 我来答
硪丨暧恋
2017-08-03 · TA获得超过8980个赞
知道大有可为答主
回答量:5336
采纳率:93%
帮助的人:2171万
展开全部
使用@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 }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式