首页 > 编程语言 >SpringMvc源码执行流程

SpringMvc源码执行流程

时间:2022-09-23 22:35:33浏览次数:51  
标签:core catalina web SpringMvc 流程 springframework 源码 apache org

Mvc源码流程

源码基于spring5.3.22

首先写一个SpringBoot工程,加一个controller,在controller打一个断点,收集堆栈:

TestAccess3$original$zyabwkcd:15, TestMvc (com.zanpo.it.threadtest.controller)
TestAccess3$original$zyabwkcd$accessor$GBxT0UEU:-1, TestMvc (com.zanpo.it.threadtest.controller)
call:-1, TestMvc$auxiliary$fhWmZ1tA (com.zanpo.it.threadtest.controller)
intercept:86, InstMethodsInter (org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance)
TestAccess3:-1, TestMvc (com.zanpo.it.threadtest.controller)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
doInvoke:205, InvocableHandlerMethod (org.springframework.web.method.support)
invokeForRequest:150, InvocableHandlerMethod (org.springframework.web.method.support)
invokeAndHandle:117, ServletInvocableHandlerMethod (org.springframework.web.servlet.mvc.method.annotation)
invokeHandlerMethod:895, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handleInternal:808, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handle:87, AbstractHandlerMethodAdapter (org.springframework.web.servlet.mvc.method)
doDispatch:1070, DispatcherServlet (org.springframework.web.servlet)
doService:963, DispatcherServlet (org.springframework.web.servlet)
processRequest:1006, FrameworkServlet (org.springframework.web.servlet)
doGet:898, FrameworkServlet (org.springframework.web.servlet)
service:655, HttpServlet (javax.servlet.http)
service:883, FrameworkServlet (org.springframework.web.servlet)
service:764, HttpServlet (javax.servlet.http)
internalDoFilter:227, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilter:53, WsFilter (org.apache.tomcat.websocket.server)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:100, RequestContextFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:93, FormContentFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:201, CharacterEncodingFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
invoke:197, StandardWrapperValve (org.apache.catalina.core)
invoke:97, StandardContextValve (org.apache.catalina.core)
invoke:541, AuthenticatorBase (org.apache.catalina.authenticator)
invoke$original$q2elQUIO:135, StandardHostValve (org.apache.catalina.core)
invoke$original$q2elQUIO$accessor$ryie5LR2:-1, StandardHostValve (org.apache.catalina.core)
call:-1, StandardHostValve$auxiliary$dpQacn9q (org.apache.catalina.core)
intercept:86, InstMethodsInter (org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance)
invoke:-1, StandardHostValve (org.apache.catalina.core)
invoke:92, ErrorReportValve (org.apache.catalina.valves)
invoke:78, StandardEngineValve (org.apache.catalina.core)
service:360, CoyoteAdapter (org.apache.catalina.connector)
service:399, Http11Processor (org.apache.coyote.http11)
process:65, AbstractProcessorLight (org.apache.coyote)
process:890, AbstractProtocol$ConnectionHandler (org.apache.coyote)
doRun:1789, NioEndpoint$SocketProcessor (org.apache.tomcat.util.net)
run:49, SocketProcessorBase (org.apache.tomcat.util.net)
runWorker:1191, ThreadPoolExecutor (org.apache.tomcat.util.threads)
run:659, ThreadPoolExecutor$Worker (org.apache.tomcat.util.threads)
run:61, TaskThread$WrappingRunnable (org.apache.tomcat.util.threads)
run:748, Thread (java.lang)

首先进入的是Thread类的run方法,这里的线程其实就是Tomcat通过事件循环+线程池实现的对每个请求的分发,是tomcat线程池中的一个线程,再继续是一些tomcat包里面的一些方法的转发。

Tomcat包会首先执行在在Web容器中注册的一些Filter类,包括一些Spring自带的Filter,然后是一些我们在Spring中自定义的Filter;接着会执行到Tomcat的Web容器中注册的Servelet,主要是Spring的DispatchServlet,然后执行doDispatch方法找到对应的Mapping方法执行。

Spring IOC容器初始化,会收集所有的Controller类中Mapping方法。封装成RequestMappingInfo对象和HandlerMethod对象。

建立一个key为RequestMappingInfo,value为HandlerMethod的Map,通过执行HandlerMethod对象中method对象的invoke方法执行对应的请求方法。

1、Spring怎么找到对应请求方法

1、无参数的Mapping

Spring里面有一个Map(m1),存放着key为请求路径,value为ReuqestMappingInfo列表。保存着对应请求路径的condition信息,通过请求路径找到这些个ReuqestMappingInfo后,通过调用他们的一些condition方法,判断这个ReuqestMappingInfo是否符合请求。

还有一个Map(m2),存放着key为ReuqestMappingInfo对象,Value为HandlerMethodMapping对象。

HandlerMethodMapping对象中存放一个HandlerMethod对象

Spring首先从第一个Map找到RequestMappingInfo对象,再从另外一个Map里面找到HandlerMethodMapping对象,然后调用其中的HandlerMethod中的Method对象反射Invole执行对应请求方法。

我们打个断点在DispatchServlet的doDispatch,通过IDEA的Evalute方法执行:

Map<String, List<RequestMappingInfo> m1 = ((RequestMappingHandlerMapping) this.handlerMappings.get(0)).mappingRegistry.pathLookup;
Map m2 = ((RequestMappingHandlerMapping) this.handlerMappings.get(0)).mappingRegistry.registry;
HandlerMethod hm = ((AbstractHandlerMethodMapping.MappingRegistration)m2.get(m1.get("/testMvc").get(0))).handlerMethod;
hm.method.invoke(webApplicationContext.getBean(hm.bean),null);

代码对应上述说的流程,就可以执行找到并执行对应的Controller方法了

2、有参数的Mapping

对于所有的ReuqestMappingInfo还会在另外一个Map中存放,上面无参数的Mapping存放的map可以加快速度,对于有参数请求会遍历所有的ReuqestMappingInfo,调用各自的condition方法。然后找到HandlerMethod对象,然后反射执行对应的方法。

2、Spring中关于请求参数和响应结果的封装

对于HandlerMethod对象中,有个MethodParameter[],代表着这个请求的参数列表,包含参数的一些定义信息:参数名,参数类型等。

然后通过从HttpServeletRequest对象中依据参数的定义,初始化对应的一个参数对象,反射执行时传入对应的参数对象。

在执行完后,依据返回的数据类型,向httpResponse对象写入。堆栈如下:

writeInternal:127, StringHttpMessageConverter (org.springframework.http.converter)
writeInternal:44, StringHttpMessageConverter (org.springframework.http.converter)
write:227, AbstractHttpMessageConverter (org.springframework.http.converter)
writeWithMessageConverters:293, AbstractMessageConverterMethodProcessor (org.springframework.web.servlet.mvc.method.annotation)
handleReturnValue:183, RequestResponseBodyMethodProcessor (org.springframework.web.servlet.mvc.method.annotation)
handleReturnValue:78, HandlerMethodReturnValueHandlerComposite (org.springframework.web.method.support)
invokeAndHandle:135, ServletInvocableHandlerMethod (org.springframework.web.servlet.mvc.method.annotation)
invokeHandlerMethod:895, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handleInternal:808, RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation)
handle:87, AbstractHandlerMethodAdapter (org.springframework.web.servlet.mvc.method)
doDispatch:1070, DispatcherServlet (org.springframework.web.servlet)
doService:963, DispatcherServlet (org.springframework.web.servlet)
processRequest:1006, FrameworkServlet (org.springframework.web.servlet)
doGet:898, FrameworkServlet (org.springframework.web.servlet)
service:655, HttpServlet (javax.servlet.http)
service:883, FrameworkServlet (org.springframework.web.servlet)
service:764, HttpServlet (javax.servlet.http)
internalDoFilter:227, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilter:53, WsFilter (org.apache.tomcat.websocket.server)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:100, RequestContextFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:93, FormContentFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
doFilterInternal:201, CharacterEncodingFilter (org.springframework.web.filter)
doFilter:117, OncePerRequestFilter (org.springframework.web.filter)
internalDoFilter:189, ApplicationFilterChain (org.apache.catalina.core)
doFilter:162, ApplicationFilterChain (org.apache.catalina.core)
invoke:197, StandardWrapperValve (org.apache.catalina.core)
invoke:97, StandardContextValve (org.apache.catalina.core)
invoke:541, AuthenticatorBase (org.apache.catalina.authenticator)
invoke$original$VBSd07m0:135, StandardHostValve (org.apache.catalina.core)
invoke$original$VBSd07m0$accessor$o3HM3lBR:-1, StandardHostValve (org.apache.catalina.core)
call:-1, StandardHostValve$auxiliary$6sdOHEfU (org.apache.catalina.core)
intercept:86, InstMethodsInter (org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance)
invoke:-1, StandardHostValve (org.apache.catalina.core)
invoke:92, ErrorReportValve (org.apache.catalina.valves)
invoke:78, StandardEngineValve (org.apache.catalina.core)
service:360, CoyoteAdapter (org.apache.catalina.connector)
service:399, Http11Processor (org.apache.coyote.http11)
process:65, AbstractProcessorLight (org.apache.coyote)
process:890, AbstractProtocol$ConnectionHandler (org.apache.coyote)
doRun:1789, NioEndpoint$SocketProcessor (org.apache.tomcat.util.net)
run:49, SocketProcessorBase (org.apache.tomcat.util.net)
runWorker:1191, ThreadPoolExecutor (org.apache.tomcat.util.threads)
run:659, ThreadPoolExecutor$Worker (org.apache.tomcat.util.threads)
run:61, TaskThread$WrappingRunnable (org.apache.tomcat.util.threads)
run:748, Thread (java.lang)

标签:core,catalina,web,SpringMvc,流程,springframework,源码,apache,org
From: https://www.cnblogs.com/zanpocc/p/16724534.html

相关文章