2023.03.20 SpringMVC源码解析
2023-03-27 21:18:43 8 举报
SpringMVC源码解析
作者其他创作
大纲/内容
adapter.supports(handler)
HandlerFunctionAdapter
Http请求,从Tomcat到SpringMVC的调用栈
ServletWebServerApplicationContext#onRefresh时(ServletWebServerFactoryConfiguration$EmbeddedTomcat)创建tomcatServletWebServerFactory
加载DispatcherServlet的Beandefinition
FrameworkServlet
font color=\"ff0011\
StandardWrapper.allocate()
HttpServletBean
SimpleControllerHandlerAdapter
init()
主要用来获取web.xml中配置的filter中设置init-param的的值,然后在将这些值赋值给对配置的filter中的属性。
returnValueHandlers = {HandlerMethodReturnValueHandlerComposite@6208} returnValueHandlers = {ArrayList@6209} size = 15 0 = {ModelAndViewMethodReturnValueHandler@8071} 1 = {ModelMethodProcessor@6213} 2 = {ViewMethodReturnValueHandler@8072} 3 = {ResponseBodyEmitterReturnValueHandler@8073} 4 = {StreamingResponseBodyReturnValueHandler@8074} 5 = {HttpEntityMethodProcessor@8075} 6 = {HttpHeadersReturnValueHandler@8076} 7 = {CallableMethodReturnValueHandler@8077} 8 = {DeferredResultMethodReturnValueHandler@8078} 9 = {AsyncTaskMethodReturnValueHandler@8079} 10 = {ServletModelAttributeMethodProcessor@8080} 11 = {RequestResponseBodyMethodProcessor@8081} 12 = {ViewNameMethodReturnValueHandler@8082} 13 = {MapMethodProcessor@8083} 14 = {ServletModelAttributeMethodProcessor@8084}
DispatcherServlet
processedRequest = checkMultipart(request);
SpringMVC内置Filter
HttpServlet
resolvers = {HandlerMethodArgumentResolverComposite@7585} argumentResolvers = {ArrayList@8098} size = 27 0 = {RequestParamMethodArgumentResolver@8101} 1 = {RequestParamMapMethodArgumentResolver@8102} 2 = {PathVariableMethodArgumentResolver@8103} 3 = {PathVariableMapMethodArgumentResolver@8104} 4 = {MatrixVariableMethodArgumentResolver@8105} 5 = {MatrixVariableMapMethodArgumentResolver@8106} 6 = {ServletModelAttributeMethodProcessor@8107} 7 = {RequestResponseBodyMethodProcessor@8108} 8 = {RequestPartMethodArgumentResolver@8109} 9 = {RequestHeaderMethodArgumentResolver@8110} 10 = {RequestHeaderMapMethodArgumentResolver@8111} 11 = {ServletCookieValueMethodArgumentResolver@8112} 12 = {ExpressionValueMethodArgumentResolver@8113} 13 = {SessionAttributeMethodArgumentResolver@8114} 14 = {RequestAttributeMethodArgumentResolver@8115} 15 = {ServletRequestMethodArgumentResolver@8116} 16 = {ServletResponseMethodArgumentResolver@8117} 17 = {HttpEntityMethodProcessor@8118} 18 = {RedirectAttributesMethodArgumentResolver@8119} 19 = {ModelMethodProcessor@8120} 20 = {MapMethodProcessor@8121} 21 = {ErrorsMethodArgumentResolver@8122} 22 = {SessionStatusMethodArgumentResolver@8123} 23 = {UriComponentsBuilderMethodArgumentResolver@8124} 24 = {PrincipalMethodArgumentResolver@8125} 25 = {RequestParamMethodArgumentResolver@8126} 26 = {ServletModelAttributeMethodProcessor@8127}
// 参数解析InvocableHandlerMethod#getMethodArgumentValuesHandlerMethodArgumentResolverComposite#resolveArgumentHandlerMethodArgumentResolver#resolveArgumentHttpMessageConverter#read
OncePerRequestFilter
ViewResolver
SpringMVC源码解析版本 v5.3.25日期:2023.03.20
SimpleServletHandlerAdapter
// 入参是processedRequest,根据请求,获取Handler// 具体来说,是根据请求的path直接匹配出RequestMappingInfo。再根据request匹配RequestMappingInfo中的methodsCondition、paramsCondition、headersCondition、consumesCondition、producesCondition、pathPatternsCondition、patternsCondition、customConditionHolder等属性值,获得匹配的RequestMappingInfo,并根据规则,得到bestMatch的HandlerMethod。然后和HandlerInterceptor组装成HandlerExecutionChain。HandlerExecutionChain mappedHandler = getHandler(processedRequest);this.handlerMappings = {ArrayList@7321} size = 5 0 = {RequestMappingHandlerMapping@9816} 1 = {BeanNameUrlHandlerMapping@9817} 2 = {RouterFunctionMapping@9818} 3 = {SimpleUrlHandlerMapping@8401} 4 = {WelcomePageHandlerMapping@9819}
接收到第一次Http请求
启动加载
servlet.init(facade)
onRefresh()-initStrategies()
converter.canWrite()
initFrameworkServlet()
第一次请求时init初始化
handler instanceof HandlerFunction
保证每个filter只执行一次
RequestResponseBodyMethodProcessor
this.localeResolver.resolveLocale(request)
GenericFilterBean
protected void initStrategies(ApplicationContext context) { initMultipartResolver(context); initLocaleResolver(context); initThemeResolver(context); initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); }
// 获取支持Handler(HandlerMethod)的HandlerAdapterHandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());adapter.supports(handler)this.handlerAdapters = {ArrayList@7322} size = 4 0 = {RequestMappingHandlerAdapter@10607} 1 = {HandlerFunctionAdapter@10608} 2 = {HttpRequestHandlerAdapter@8295} 3 = {SimpleControllerHandlerAdapter@10609}
DispatcherServletRegistrationBean
请求处理
RequestMappingHandlerAdapter#invokeHandlerMethod
第一次请求时init初始化过程
handler instanceof HttpRequestHandler
AbstractHandlerMethodAdapter#supports(handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler))
HandlerAdapter
converter.write()
Servlet
使用自动配置加载流程,加载Beandefinition
ErrorPageCustomizer
Tomcat架构ApplicationFilterChain
HttpMessageConverter
handler instanceof Controller
initServletBean()
this.messageConverters = {ArrayList@7635} size = 10 0 = {ByteArrayHttpMessageConverter@8011} 1 = {StringHttpMessageConverter@8015} 2 = {StringHttpMessageConverter@8016} 3 = {ResourceHttpMessageConverter@8017} 4 = {ResourceRegionHttpMessageConverter@8018} 5 = {SourceHttpMessageConverter@8019} 6 = {AllEncompassingFormHttpMessageConverter@8020} 7 = {MappingJackson2HttpMessageConverter@8021} 8 = {MappingJackson2HttpMessageConverter@8022} 9 = {Jaxb2RootElementHttpMessageConverter@8023}
GenericServlet
创建DispatcherServlet的bean
⭐️2023.04.23 SpringBoot自动配置源码解析https://www.processon.com/view/link/6444f93b84b4b71c14ff3fa3
CharacterEncodingFilterCommonsRequestLoggingFilterCorsFilterFormContentFilterForwardedHeaderFilterHiddenHttpMethodFilterHttpPutFormContentFilterMultipartFilterOrderedFormContentFilterRelativeRedirectFilterRequestContextFilterServletContextRequestLoggingFilterShallowEtagHeaderFilter
RequestMappingHandlerAdapter
HttpRequestHandlerAdapter
@InitBinder、@ModelAttribute
onRefresh()
handler instanceof Servlet
自动配置加载流程-DispatcherServlet1、启动时,根据以下时序图加载DispatcherServletAutoConfiguration2、DispatcherServletConfiguration中通过@Bean声明了DispatcherServlet3、此处仅通过构造方法创建了bean对象
ThemeResolver
this.viewResolvers = {ArrayList@6514} size = 4 0 = {ContentNegotiatingViewResolver@8449} 1 = {BeanNameViewResolver@8454} 2 = {ViewResolverComposite@8455} 3 = {InternalResourceViewResolver@8456}
initWebApplicationContext()-configureAndRefreshWebApplicationContext()-onRefresh()
ErrorPageRegistrarBeanPostProcessor
Bean type ExplanationHandlerMapping \
FlashMapManager
MultipartResolver
HandlerExceptionResolver
HandlerMapping
核心组件
AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization
0 条评论
下一页