DispatcherServlet與ContextLoaderListener

DispatcherServlet與ContextLoaderListener

Spring MVC 配置的兩種方式:Web.xml 和 java 類配置

DispatcherServlet是Spring MVC的核心,在這裏請求會第一次接觸到框架,它負責將請求路由到其餘組件之中。php

傳統配置DispatcherServlet是採用web.xml文件的方式。通常以下:css

<?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">
    
     <!-- 監聽器配置 -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 上下文參數,在監聽器中被使用 -->
    <!-- 監聽器配置文件路徑 -->
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>
        	classpath:applicationContext.xml
        </param-value>
    </context-param>
    
    <!-- 前端控制器配置 -->
    <servlet>
        <!--在DispatcherServlet的初始化過程當中,框架會在web應用的WEB-INF文件夾下默認尋找名爲[servlet-name]-servlet.xml 的配置文件,生成文件中定義的bean。如如下默認尋找dispatcher-servlet.xml-->
    	<servlet-name>dispatcher</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<init-param>
            <!-- 前端控制器配置文件路徑 -->
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext-mvc.xml</param-value>
    	</init-param>
        <!--是啓動順序,讓這個Servlet隨Servletp容器一塊兒啓動。-->
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <!--這個Servlet的名字是dispatcher,能夠有多個DispatcherServlet,是經過名字來區分的。每個DispatcherServlet有本身的WebApplicationContext上下文對象。同時保存的ServletContext中和Request對象中.-->
        <!--ApplicationContext是Spring的核心,Context咱們一般解釋爲上下文環境,我想用「容器」來表述它更容易理解一些,ApplicationContext則是「應用的容器」了:P,Spring把Bean放在這個容器中,在須要的時候,用getBean方法取出-->
    	<servlet-name>dispatcher</servlet-name>
        <!--Servlet攔截匹配規則能夠自已定義,當映射爲@RequestMapping("/user/add")時,爲例,攔截哪一種URL合適?-->
        <!--一、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會致使靜態文件(jpg,js,css)被攔截。-->
        <!--二、攔截/,例如:/user/add,能夠實現如今很流行的REST風格。不少互聯網類型的應用很喜歡這種風格的URL。弊端:會致使靜態文件(jpg,js,css)被攔截後不能正常顯示。 -->
    	<url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
複製代碼

在Servlet3規範中,容器會在類路徑中查找實現ServletContainerInitializer接口的類,若是能發現的話就用它來配置Servlet容器。前端

Spring中提供了這個接口的實現,名爲SpringServletContainerInitializer,這個類反過來又會查找實現WebApplicationInitializer的類並將配置任務交給它們繼續完成。Spring3.2中,提供了一個便利的WebApplicationInitializer的基礎實現,即AbstractAnnotationConfigDispatcherServletInitializer。因此採用java配置DispatcherServlet只須要擴展AbstractAnnotationConfigDispatcherServletInitializer就能夠了。java

而且擴展AbstractAnnotationConfigDispatcherServletInitializer的任意類都會自動的配置DispatcherServlet和SpringApplicationContext。web

通常java配置以下:spring

public class WebMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    /** * 根應用上下文配置文件 */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{
            RootConfig.class
        };
    }

    /** * web 應用上下文配置文件 */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{
        	WebConfig.class
        };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{
                "/"
        };
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new SessionListener());
    }
}
複製代碼

兩個應用上下文之間的故事

在java配置文件中,咱們發現了兩個應用上下文:根應用上下文,web應用上下文。spring-mvc

首先咱們先看 DispatcherServlet 和 ContextLoaderListener。tomcat

DispatcherServlet ,前端控制器

DispatcherServlet 本質是一個Servlet,它擴展了HTTPServlet,其主要目的是處理與配置URL模式匹配的傳入Web的請求。安全

每個DispatcherServlet定義一個Spring的web application,而且都與一個WebApplicationContext相關。當DispatcherServlet啓動的時候,它會建立WebApplicationContext,並加載配置文件。mvc

從Spring 3.x開始,方法DispatcherServlet(WebApplicationContext webApplicationContext)經過給定的web application context建立一個新DispatcherServlet。只有在Servlet 3.x環境中才有可能經過ServletContext.addServlet(java.lang.String, java.lang.String)的API支持。

ContextLoaderListener,上下文加載監聽器

ContextLoaderListener是spring框架對servlet監聽器的一個封裝,本質上仍是一個servlet監聽器,它建立了一個根應用程序上下文(ApplicationContext),並與全部DispatcherServlet上下文建立的子上下文共享。

ContextLoaderListener 包含全局可見的bean 的上下文,如服務,存儲庫,基礎結構bean等。建立根應用程序上下文後,它將 ServletContext 做爲屬性存儲,名稱爲:

// org/springframework/web/context/ContextLoader.java
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
 
//Where attibute is defined in /org/springframework/web/context/WebApplicationContext.java as
 
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
複製代碼

在Spring控制器中獲取根應用程序上下文,可使用 WebApplicationContextUtils 類。

// Controller.java
@Autowired
ServletContext context;
 
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);
 
if(ac == null){
    return "root application context is null";
}    
複製代碼

在Spring Web應用程序中,有兩種類型的容器,ApplicationContext和WebAppilicationContext.

ApplicationContext是由ContextLoaderListener建立並配置的或Web.xml。

而WebApplicationContext是ApplicationContext的子上下文環境。是由DispatcherServlet 啓動時建立配置的

DispatcherServlet 和 ContextLoaderListener之間的關係。

  因此說若是沒有配置listener參數,只配置了dispatcherServlet時,tomcat啓動時是不會初始化Spring Web上下文的,由於Spring Web是基於Spring的,你沒有配置Spring,因此也不會啓動它的子上下文Spring Web。這一點在Spring MVC啓動源碼中也能夠看到。

下圖中詳細的描述了整個關係。

ContextLoaderListener與DispatcherServlet

  1. ContextLoaderListener 建立根應用程序上下文
  2. DispatcherServlet 條目爲每一個servlet條目建立一個子應用程序上下文。
  3. 子上下文能夠訪問根上下文中定義的bean。
  4. 根上下文中的Bean沒法直接訪問子上下文中的bean。
  5. 全部上下文都被添加到ServletContext
  6. 你可使用WebApplicationContextUtils類訪問根上下文。

兩者具體關係參考自ContextLoaderListener vs DispatcherServlet

Spring MVC啓動流程

Tomcat啓動時會優先加載Servlet監聽器組件,並調用ContextInitialized方法。而ContextLoaderListener繼承了ServletContextListener實現了ContextInitialized方法。在其中調用initwebApplicationContext方法初始化Spring Web Context。這就是 Spring MVC 的入口。

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

   public ContextLoaderListener() {
   }
   
   public ContextLoaderListener(WebApplicationContext context) {
      super(context);
   }

   /** * Initialize the root web application context. */
   @Override
   public void contextInitialized(ServletContextEvent event) {
      initWebApplicationContext(event.getServletContext());
   }

   /** * Close the root web application context. */
   @Override
   public void contextDestroyed(ServletContextEvent event) {
      closeWebApplicationContext(event.getServletContext());
      ContextCleanupListener.cleanupAttributes(event.getServletContext());
   }
}
複製代碼

通常DispatcherServlet加載包含Web組件的bean(控制器,視圖解析器,以及處理器映射),根上下文加載應用中的其餘bean(安全性,事務,服務,中間層和數據層組件等)。

參考文獻:

howtodoinjava.com/spring-mvc/…

blog.csdn.net/seagal890/a…

juejin.im/post/5b207d…

相關文章
相關標籤/搜索