问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

springmvc中的requestmapping注解怎么实现的

发布网友 发布时间:2022-04-26 03:07

我来回答

1个回答

热心网友 时间:2022-06-20 08:56

使用@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 ProcesRequestCondition procesCondition;
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 ProcesRequestCondition proces = this.procesCondition.combine(other.procesCondition);
13 RequestConditionHolder custom = this.customConditionHolder.combine(other.customConditionHolder);
14
15 return new RequestMappingInfo(patterns, methods, params, headers, consumes, proces, 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 ProcesRequestCondition proces = procesCondition.getMatchingCondition(request);
14
15 if (methods == null || params == null || headers == null || consumes == null || proces == 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, proces, 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 = procesCondition.compareTo(other.getProcesCondition(), 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 }
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
手机导航地图语音怎么下载 如何分别真金和仿金首饰 怎样区分真金和仿金首饰呢 小学生新年晚会主持人的串词!!(不要太多)急 大大后天就需要了!!!_百度... 周年晚会策划公司 奥格瑞玛传送门大厅在哪 奥格瑞玛传送门大厅怎么走 锻炼颈椎的几个动作 水多久能结冰 冰能在多长时间内形成 请问水低于0度会结冰吗? 如何防止脱发严重 有关于告诫爱吃肉的人不可过量吃肉也不可偏食只爱吃肉的句子 spring基于注解怎么实现的 偷吃肉格言 Java自定义注解注解实现实体类与数据库表字段的映射 吃肉要吃蒜下一句 吃肉造句不能超过8个字 如何实现自定义注解 大口喝酒大口吃肉有一句诗句来形容 如何使用注解实现activemq的事务原理 电脑触控板怎么禁用 spring注解是怎么实现的 形容爱吃肉的经典句子有哪些? java 注解是怎么实现的 大口吃肉的幽默说说 怎样通过注解找到对应的源码实现类 形容吃肉的句子 spring基于注解怎么实现的?常用的注解有哪些 spring的注解是怎么实现的 java中注解原理 吃肉的句子发朋友圈 佛门的一句话,和尚吃肉的句怎么说来?看详细提问。 小乌龟吃肉的好词好句 狼行千里吃肉下一句是什么 盐水虾的最好做法 盐水虾的做法 盐水基围虾的做法有哪些? 想煮盐水基围虾,需要去虾线吗?如果去的话怎么去? 盐水虾的做法,盐水虾怎么做好吃,盐水虾的家常 盐水虾的做法 盐水虾怎么做 安装窗帘时都会选择安装窗帘盒,你觉得是为什么呢? 装修房子的时候,怎么安装窗帘盒? 窗帘盒一般什么尺寸/留多宽,多高就可以安装了 窗帘盒安装步骤是什么,注意什么?会不会很复杂? 安装窗帘窗帘盒有哪些步骤 什么是吸顶式窗帘盒和悬挂式窗帘盒? 窗帘盒到底是什么 什么是窗帘盒 窗帘盒安装步骤及注意事项 家里装修想装窗帘,请问窗帘盒的内部基层该怎么做? 没有吊顶,可以安装窗帘盒吗? 窗帘盒有没有必要安装?