概述
最開始使用原生的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的配置文件所在目錄。默認(rèn)配置在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規(guī)范
首先研究的第一點奸腺,為什么web.xml不見了。剛開始使用原生servlet(不使用web框架)血久,web.xml就是非常重要的一個配置突照,無論是servlet、filter氧吐、listener都需要在web.xml里面配置下讹蘑。
但是在servlet3.0里,這個配置得到了簡化筑舅∽浚可以通過java配置(注解等)省去web.xml配置。
具體servlet3.0的規(guī)范這里就不討論了翠拣,說下其中一個非常重要的類版仔。javax.servlet.ServletContainerInitializer
這個類會在web容器啟動階段被回調(diào),可以在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的實現(xiàn)
首先spring在META-INF/services下配置了這個類谜慌,讓整個web容器啟動后可以找到并啟動這個類
SpringServletContainerInitializer
/**
* @HandlesTypes這個注解標(biāo)明了該ServletContainerInitializer需要在啟動時候處理哪些類蝉揍,
然后服務(wù)器會把找到的這些類傳到onStartup的第一個參數(shù)里
注意這里的類包括所配置類的子類,比如這里配置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容器啟動后熙卡,調(diào)用所有WebApplicationInitializer的onStartup方法杖刷。
WebApplicationInitializer 的實現(xiàn)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應(yīng)用部署到機器上,tomcat能夠找到springboot的入口甜滨,并啟動它乐严。
DispatcherServlet的配置
關(guān)于springboot如何加載類并啟動的這里就不介紹了。
這里說明下究竟Springboot如何配置DispatcherServlet的
1)當(dāng)類路徑下存在DispatcherServlet時候衣摩,該配置生效昂验。
2)這個配置會在DispatcherServletAutoConfiguration配置完之后再配置莹桅。
DispatcherServletAutoConfiguration配置
看到這里就是我們非常熟悉的springboot的使用了踩寇。springboot在DispatcherServletConfiguration這個類里對DispatcherServlet進行了配置以及注冊。
總結(jié)
服務(wù)器如tomcat在web應(yīng)用啟動后,加載并啟動springboot唐断,springboot通過@AutoConfiguration骡技、@Bean擎颖、@Conditional等注解自動配置了DispatcherServlet蜀肘。