概述
最開(kāi)始使用原生的springmvc時(shí),總是免不了有如下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的配置文件所在目錄。默認(rèn)配置在WEB-INF目錄下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
但是,切換到springboot之后,web.xml之類的繁瑣的配置基本上都不見(jiàn)了。出于好奇研究了下springboot究竟幫我們做了什么,我們可以免于這樣的繁瑣配置。
Servlet3.0規(guī)范
首先研究的第一點(diǎn),為什么web.xml不見(jiàn)了。剛開(kāi)始使用原生servlet(不使用web框架),web.xml就是非常重要的一個(gè)配置,無(wú)論是servlet、filter、listener都需要在web.xml里面配置下。
但是在servlet3.0里,這個(gè)配置得到了簡(jiǎn)化。可以通過(guò)java配置(注解等)省去web.xml配置。
具體servlet3.0的規(guī)范這里就不討論了,說(shuō)下其中一個(gè)非常重要的類。javax.servlet.ServletContainerInitializer
這個(gè)類會(huì)在web容器啟動(dòng)階段被回調(diào),可以在onStartup方法里做一些servlet、filter、listener的注冊(cè)等操作。
/**
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的實(shí)現(xiàn)
首先spring在META-INF/services下配置了這個(gè)類,讓整個(gè)web容器啟動(dòng)后可以找到并啟動(dòng)這個(gè)類

SpringServletContainerInitializer
/**
* @HandlesTypes這個(gè)注解標(biāo)明了該ServletContainerInitializer需要在啟動(dòng)時(shí)候處理哪些類,
然后服務(wù)器會(huì)把找到的這些類傳到onStartup的第一個(gè)參數(shù)里
注意這里的類包括所配置類的子類,比如這里配置WebApplicationInitializer,
啟動(dòng)之后,就會(huì)把這個(gè)WebApplicationInitializer的子類都傳進(jìn)去
*/
@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>();
//.... 省略容錯(cuò)的一些代碼
initializers.add((WebApplicationInitializer) waiClass.newInstance());
//....
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
}
startup的邏輯很簡(jiǎn)單,web容器啟動(dòng)后,調(diào)用所有WebApplicationInitializer的onStartup方法。
WebApplicationInitializer 的實(shí)現(xiàn)SpringBootServletInitializer
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//....
WebApplicationContext rootAppContext = createRootApplicationContext(
servletContext);
//...
}
protected WebApplicationContext createRootApplicationContext(
ServletContext servletContext) {
//...
return run(application);
}
一般使用Springboot的時(shí)候,都會(huì)繼承一個(gè)類SpringBootServletInitializer,在這個(gè)類的onStartup方法中,啟動(dòng)了整個(gè)Spring容器。
本地啟動(dòng)springboot時(shí),我們一般會(huì)寫(xiě)一個(gè)類似于這樣的main方法。

上述分析也解釋了為啥把springboot應(yīng)用部署到機(jī)器上,tomcat能夠找到springboot的入口,并啟動(dòng)它。
DispatcherServlet的配置
關(guān)于springboot如何加載類并啟動(dòng)的這里就不介紹了。
這里說(shuō)明下究竟Springboot如何配置DispatcherServlet的

1)當(dāng)類路徑下存在DispatcherServlet時(shí)候,該配置生效。
2)這個(gè)配置會(huì)在DispatcherServletAutoConfiguration配置完之后再配置。
DispatcherServletAutoConfiguration配置


看到這里就是我們非常熟悉的springboot的使用了。springboot在DispatcherServletConfiguration這個(gè)類里對(duì)DispatcherServlet進(jìn)行了配置以及注冊(cè)。
總結(jié)
服務(wù)器如tomcat在web應(yīng)用啟動(dòng)后,加載并啟動(dòng)springboot,springboot通過(guò)@AutoConfiguration、@Bean、@Conditional等注解自動(dòng)配置了DispatcherServlet。
