SpringMVC啓動過程詳解

 經過對SpringMVC啓動過程的深刻研究,指望掌握Java Web容器啓動過程;掌握SpringMVC啓動過程;瞭解SpringMVC的配置文件如何配置,爲何要這樣配置;掌握SpringMVC是如何工做的;掌握Spring源碼的設計和加強閱讀源碼的技巧。前端

目錄java

1.Web容器初始化過程web

2.SpringMVC中web.xml配置spring

3.認識ServletContextListener架構

4.認識ContextLoaderListenerapp

5.DispatcherServlet初始化(HttpServletBean • FrameworkServlet • DispatcherServletide

6.ContextLoaderListener與DispatcherServlet關係this

7.DispatcherServlet的設計spa

8.DispatcherServlet工做原理設計

 

1、Web容器初始化過程

上圖展現了web容器初始化的過程,其官方文檔給出了這樣的描述:

  When a web application is deployed into a container, the following steps must be performed, in this order, before the web application begins processing client requests.

  1. Instantiate an instance of each event listener identified by a <listener> element in the deployment descriptor.
  2. For instantiated listener instances that implement ServletContextListener, call the contextInitialized() method.
  3. Instantiate an instance of each filter identified by a <filter> element in the deployment descriptor and call each filter instance's init() method.
  4. Instantiate an instance of each servlet identified by a <servlet> element that includes a <load-on-startup> element in the order defined by the load-on-startup element values, and call each servlet instance's init() method.

2、SpringMVC中web.xml的配置

上圖是截取的web.xml中的配置,在<listener>標籤中定義了spring容器加載器;在<servlet>標籤中定義了spring前端控制器。

上圖是源碼中接口ServletContextListener的定義,能夠看到在其註釋中指明:servlet和Filter初始化前和銷燬後,都會給實現了servletContextListener接口的監聽器發出相應的通知。

上面是類ContextLoadListener的定義,它實現了上面的servletContextListener。這裏用到了代理模式,簡單的代理了ContextLoader類。ContextLoadListener類用來建立Spring application context,而且將application context註冊到servletContext裏面去。

結合上面的WEB容器啓動的過程,以及接口ServletContextListener和類ContextLoadListener。咱們知道:

  在 Servlet API中有一個ServletContextListener接口,它可以監聽ServletContext對象的生命週期,實際上就是監聽Web應用的生命週期。當Servlet容器啓動或終止Web應用時,會觸發ServletContextEvent事件,該事件由ServletContextListener來處理。在ServletContextListener接口中定義了處理ServletContextEvent 事件的兩個方法contextInitialized()和contextDestroyed()。

  ContextLoaderListener監聽器的做用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。由於它實現了ServletContextListener這個接口,在web.xml配置了這個監聽器,啓動容器時,就會默認執行它實現的方法。因爲在ContextLoaderListener中關聯了ContextLoader這個類,因此整個加載配置過程由ContextLoader來完成。

上面是initWebApplicationContext的過程,方法名稱便是其含義。方法中首先建立了WebApplicationContext,配置而且刷新實例化整個SpringApplicationContext中的Bean。所以,若是咱們的Bean配置出錯的話,在容器啓動的時候,會拋異常出來的。

  綜上,ContextLoaderListener類起着相當重要的做用。它讀取web.xml中配置的context-param中的配置文件,提早在web容器初始化前準備業務對應的Application context;將建立好的Application context放置於ServletContext中,爲springMVC部分的初始化作好準備。

3、DispatchServlet初始化

  在SpringMVC架構中,DispatchServlet負責請求分發,起到控制器的做用。下面詳細來解釋說明:

DispatchServlet名如其義,它的本質上是一個Servlet。從上面圖能夠看到,下層的子類不斷的對HttpServlet父類進行方法擴展。

上圖是抽象類HttpServletBean的實現,咱們知道HttpServlet有兩大核心方法:init()和service()方法。HttpServletBean重寫了init()方法,在這部分,咱們能夠看到其實現思路:公共的部分統一來實現,變化的部分統一來抽象,交給其子類來實現,故用了abstract class來修飾類名。此外,HttpServletBean提供了一個HttpServlet的抽象實現,使的Servlet再也不關心init-param部分的賦值,讓servlet更關注於自身Bean初始化的實現。

上圖是FrameworkServlet的官方定義, 它提供了整合web javabean和spring application context的整合方案。那麼它是如何實現的呢?在源碼中咱們能夠看到經過執行initWebApplicationContext()方法和initFrameworkServlet()方法實現。

DispatchServlet是HTTP請求的中央調度處理器,它將web請求轉發給controller層處理,它提供了敏捷的映射和異常處理機制。DispatchServlet轉發請求的核心代碼在doService()方法中實現,詳細代碼參照圖上。

上圖是DispatchServlet類和ContextLoaderListener類的關係圖。首先,用ContextLoaderListener初始化上下文,接着使用DispatchServlet來初始化WebMVC的上下文。

上圖是DispatchServlet的工做流程圖,做爲HTTP請求的中央控制器,它在SpringMVC中起着分發請求的做用。下面總結了DispatchServlet設計的一些特色總結。

4、請求流程

相關文章
相關標籤/搜索