前言
Github:https://github.com/yihonglei/thinking-in-springmvc
Spring MVC啓動流程:https://jpeony.blog.csdn.net/article/details/106793070
一 SpringMVC概述
Spring MVC封裝servlet的MVC Web開發框架,減輕直接操作Servlet的繁瑣,提高開發效率。
二 Spring MVC執行流程源碼
1、時序圖
先上一個簡易版本的。
DispatcherServlet負責請求的轉發 ------> HandlerMapping基於url查找Handler
------> HandlerAdapter基於Handler查找處理器 ------> 對應url的Handler執行業務
------> 處理結果交給ViewResolver視圖解析器盡心處理 ------> 數據返回給端上進行渲染展示,請求響應完成!
2、執行流程詳細
假設web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>springMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化參數 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 攔截所有以do結尾的請求 -->
<servlet-mapping>
<servlet-name>springMVCDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 定義映射 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="helloWorld.do">helloWorldController</prop>
</props>
</property>
</bean>
<!-- 定義視圖解析器,bean的id,property的name都是spring約定的 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>
<!-- 定義控制器 -->
<bean id="helloWorldController"
class="com.jpeony.springmvc.controller.HelloWorldController">
<property name="helloWorld">
<value>HelloWorld</value>
</property>
<property name="viewPage">
<value>/WEB-INF/jsp/index.jsp</value>
</property>
</bean>
</beans>
上面通過簡易時序圖瞭解了大概流程,下面詳細搞下請求從哪個代碼進來的,在哪個代碼處理,從哪個代碼出去的。
假設用Tomcat容器,SpringMVC包裝Servlet,應用靠web容器Tomcat啓動。
應用請求過來先到Tomcat,Tomcat 默認IO模型使用NIO模型,請求最後訪問到Servlet的service()方法。
Servlet實例及生命週期參考:https://blog.csdn.net/yhl_jxy/article/details/78969579
Spring MVC請求故事從Servlet的service()方法開始。
先看下DispatcherServlet的結構關係圖。
每一次的請求,都會訪問Servlet的service()方法,根據Servlet繼承實現關係,Spring MVC的FrameworkServlet
重寫了service()方法。
調用父類HttpServlet的service()方法。
doPost()在FrameworkServlet中實現。
調用FrameworkServlet#doService(),DispatcherServlet重寫doService()。
請求具體處理在DispatcherServlet#doDispatch()方法中,代碼有點長,分開截圖。
獲取執行責任鏈。
獲取執行適配器SimpleControllerHandlerAdapter,通過handle調用實際處理器。
繼續往下走。
這裏調用我們自己的Controller的handleRequest()方法。
久違已久的Controller,終於進來了。
在這個doDispatch()的下面部分代碼,對返回結果進行處理,對ModelAndView進行處理。
render對結果進行處理,解析視圖名稱,以及將結果塞在response返回。
終於,進到了spring-mvc.xml裏面配置的InternalResourceViewResolver視圖解析器中。
返回相應視圖地址,內容,端上解析,展現。