天天看點

springboot 是如何幫我們省去web.xml配置的

概述

最開始使用原生的springmvc時,總是免不了有如下xml配置

<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
  

<!-- Spring配置 -->
<!-- ====================================== -->
<listener>
   <listenerclass>
 org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
  

<!-- 指定Spring Bean的配置檔案所在目錄。預設配置在WEB-INF目錄下 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
           

但是,切換到springboot之後,web.xml之類的繁瑣的配置基本上都不見了。出于好奇研究了下springboot究竟幫我們做了什麼,我們可以免于這樣的繁瑣配置。

Servlet3.0規範

首先研究的第一點,為什麼web.xml不見了。剛開始使用原生servlet(不使用web架構),web.xml就是非常重要的一個配置,無論是servlet、filter、listener都需要在web.xml裡面配置下。

但是在servlet3.0裡,這個配置得到了簡化。可以通過java配置(注解等)省去web.xml配置。

具體servlet3.0的規範這裡就不讨論了,說下其中一個非常重要的類。javax.servlet.ServletContainerInitializer

這個類會在web容器啟動階段被回調,可以在onStartup方法裡做一些servlet、filter、listener的注冊等操作。

/**
Interface which allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets, filters, and listeners in response to it.
*/
public interface ServletContainerInitializer {

    public void onStartup(Set<Class<?>> c, ServletContext ctx)
        throws ServletException; 
}
           

springboot的實作

首先spring在META-INF/services下配置了這個類,讓整個web容器啟動後可以找到并啟動這個類

springboot 是如何幫我們省去web.xml配置的

ServletContainerInitializer配置

SpringServletContainerInitializer

/**
* @HandlesTypes這個注解标明了該ServletContainerInitializer需要在啟動時候處理哪些類,
然後伺服器會把找到的這些類傳到onStartup的第一個參數裡

注意這裡的類包括所配置類的子類,比如這裡配置WebApplicationInitializer,
啟動之後,就會把這個WebApplicationInitializer的子類都傳進去
*/
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
        
    @Override
    public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
            throws ServletException {

        List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();

        //.... 省略容錯的一些代碼
        initializers.add((WebApplicationInitializer) waiClass.newInstance());
        //.... 
    AnnotationAwareOrderComparator.sort(initializers);
        for (WebApplicationInitializer initializer : initializers) {
            initializer.onStartup(servletContext);
        }
    }

}
           

startup的邏輯很簡單,web容器啟動後,調用所有WebApplicationInitializer的onStartup方法。

WebApplicationInitializer 的實作SpringBootServletInitializer

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
   //....
    WebApplicationContext rootAppContext = createRootApplicationContext(
            servletContext);
   //...
}
           
protected WebApplicationContext createRootApplicationContext(
        ServletContext servletContext) {
    //...
    return run(application);
}
           

一般使用Springboot的時候,都會繼承一個類SpringBootServletInitializer,在這個類的onStartup方法中,啟動了整個Spring容器。

本地啟動springboot時,我們一般會寫一個類似于這樣的main方法。

springboot 是如何幫我們省去web.xml配置的

上述分析也解釋了為啥把springboot應用部署到機器上,tomcat能夠找到springboot的入口,并啟動它。

DispatcherServlet的配置

關于springboot如何加載類并啟動的這裡就不介紹了。

這裡說明下究竟Springboot如何配置DispatcherServlet的

springboot 是如何幫我們省去web.xml配置的

1)當類路徑下存在DispatcherServlet時候,該配置生效。

2)這個配置會在DispatcherServletAutoConfiguration配置完之後再配置。

DispatcherServletAutoConfiguration配置

springboot 是如何幫我們省去web.xml配置的
springboot 是如何幫我們省去web.xml配置的

看到這裡就是我們非常熟悉的springboot的使用了。springboot在DispatcherServletConfiguration這個類裡對DispatcherServlet進行了配置以及注冊。

總結

伺服器如tomcat在web應用啟動後,加載并啟動springboot,springboot通過@AutoConfiguration、@Bean、@Conditional等注解自動配置了DispatcherServlet。

springboot 是如何幫我們省去web.xml配置的

醜醜的櫻花星