Spring MVC
Spring MVC是基於servlet功能實現的,通過實現Servlet接口的DispatcherServlet來封裝其核心功能實現,通過將請求分派給處理程序,同時帶有可配置的處理程序映射、視圖解析、本地語言、主題解析以及上傳下載文件支持。默認的處理程序是非常簡單的Controller接口,只有一個ModelAndView hangleRequest(request, response)方法。Spring提供了一個控制器層次結構,可以派生子類。如果應用程序需要處理用戶輸入表單,那麼可以繼承AbstractFormController。如果應用程序需要處理用戶輸入表單,那麼可以繼承AbstractWizardFormController。
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring_mvc01</display-name>
<!-- 配置Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置上下文載入器 -->
<!-- 上下文載入器除DispatcherServlet載入配置文件置之外的其它上下文配置文件 -->
<!-- 最常用的上下文載入器是一個Servlet監聽器,其名稱爲ContextLoderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC的前端控制器 -->
<!-- 當DispatcherServlet載入後,它將從一個配置文件中載入Springmvc的應用上下文 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
1、ContextLoaderListener
對於SpringMVC功能的實現分析。我們首先從web.xml開始,在web.xml文件中我們首先配置的就是ContextLoaderListener。
ContextLoaderListener的作用啓動web容器時,自動裝配ApplicationContext的配置信息(spring.xml)。因爲他實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法,使用ServletContextListener這個接口,開發者能夠在爲客戶端請求提供服務之前向ServletContext中添加任意的對象。這個對象在ServletContext啓動的時候被初始化,然後在ServletContext整個運行期間都是可見的。每一個Web應用都有一個ServletContext與之相關聯。ServletContext對象在引用啓動時被創建,在引用關閉時被銷燬。ServletContext在全局範圍內有效,類似於應用中的一個全局變量。
1.1、ServletContextListener
(1)自定義實現ServletContextListener,首先我們創建ServletContextListener,目標是在系統啓動時添加自定義的屬性,以便於在全局範圍內可以隨時調用。系統啓動的時候會調用ServletContextListener實現類的contextInitialized方法,所以需要在這個方法中實現我們的初始化邏輯:
(2)註冊監聽器:在web.xml文件中需要註冊自定義的監聽器:
(3)啓動tomcat服務器,我們就能在任意的servlet或者jsp中通過下面的方式來獲取我們的初始化參數:
1.2、ContextLoaderListener
ContextLoaderListener實現了ServletContextListener接口,因此在ServletContext啓動之後會調用ServletContextListener的contextInitialized方法,那麼,我們就從這個函數開始進行分析:
這裏涉及了一個常用類WebApplicationContext;在web應用中,我們會用到WebApplicationContext,WebApplicationContext繼承自ApplicationContext,在ApplicationContext的基礎上又追加了一些特定於Web的操作及屬性。其主要實現如下:
(1)檢驗WebApplicationContext存在性,
(2)創建WebApplicationContext實例,
(3)將實例記錄在serveltContext中,
(4)映射當前的類加載器與創建的實例到全局變量currentContextPerThread中。
2、DispatcherServlet
在Spring中,ContextLoaderListener只是輔助功能,用於創建WebApplicationContext類型實例,而真正的邏輯實現其實就是在DispatcherServlet中進行的,DispatcherServlet是實現servlet接口的實現類。servlet是一個java編寫的程序,此程序是基於HTTP協議的,在服務器端運行的,是按照servelt規範編寫的一個java類。servelt主要是處理客戶端的請求並將其結果發送到客戶端。Servelt的框架是由兩個java包組成,javax.servelt和javax.servlet.http。javax.servlet定義了所有的servlet類都必須實現或擴展的通用接口和類,在javax.servle.http定義了採用HTTP通信協議的HttpServlet類。
2.1、DispatcherServlet的初始化(init() )
DispatcherServlet的初始化過程主要是通過將當前的servlet類型實例轉換爲BeanWrapper類型實例,以便使用Spring中提供的注入功能進行對應的屬性的注入。屬性注入包括:
1)封裝及驗證初始化參數,
2)將當前servlet實例轉化成BeanWrapper實例,
3)註冊相對於Resource的屬性編輯器,
4)屬性注入,
5)servletBean的初始化 。
在ContextLoaderListener加載的時候已經創建了WebApplicationContext實例,而在這個函數中最重要的就是對這個實例進一步的補充初始化:
protected final void initServletBean() throws ServletException {
Logger... //日誌處理
//對WebApplication進行補充處理
this.webApplicationContext = initWebApplicationContext();
Logger... //日誌處理
}
2.2、WebApplicationContext的初始化
initWebApplicationContext函數的主要工作就是創建或刷新WebApplicationContext實例並對servlet功能所使用的變量進行初始化。其主要包括以下幾個部分:
1)尋找或創建對應的WebApplicationContext實例
2)調用configureAndRefreshWebApplicationContext方法來對已經創建的WebApplicationContext實例進行配置及刷新
3)刷新:onRefresh是FrameworkServlet類中提供的模板方法,在其子類DispathcerServlet中進行了重寫,主要用於刷新Spring在web功能實現中所必須使用的全局變量。