目录
以下代码为 spring boot 2.7.15 中自带的 spring 5.3.29
请求方法相关信息封装,对应的信息解析在 RequestMappingHandlerMapping 的 createRequestMappingInfo() 中实现。
对于 @RequestMapping 赋值的相关信息进行解析
- protected RequestMappingInfo createRequestMappingInfo(
- RequestMapping requestMapping, @Nullable RequestCondition> customCondition) {
-
- RequestMappingInfo.Builder builder = RequestMappingInfo
- .paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
- .methods(requestMapping.method())
- .params(requestMapping.params())
- .headers(requestMapping.headers())
- .consumes(requestMapping.consumes())
- .produces(requestMapping.produces())
- .mappingName(requestMapping.name());
- if (customCondition != null) {
- builder.customCondition(customCondition);
- }
- return builder.options(this.config).build();
- }
实现类为 RequestMappingHandlerMapping
保存了请求路径与 RequestMappingInfo 的关系,最终在 AbstractHandlerMethodMapping 的内部类 MappingRegistry 中保存。
相关的路径信息在 AbstractHandlerMethodMapping 中实现,由于实现了接口 InitializingBean,在 spring ioc 容器创建完后进行 bean 初始化时进行路径解析处理,最终解析为 RequestMappingInfo 对象,保存到 AbstractHandlerMethodMapping 的内部类 MappingRegistry 中。
保存信息如下
bean
bean 名称
beanType
bean 类型
method
bridgeMethod
方法类型
实现类为 RequestMappingHandlerAdapter
最终请求处理类
handler
保存了 HandlerMethod 信息
interceptorList
HandlerInterceptor 实现类

请求过程通过 HandlerExecutionChain 中的 HandlerMethod 进行流转,最终请求在 InvocableHandlerMethod 中通过反射调用对应 controller 的方法。
