springboot默认支持tomcat、jetty、undertow作为底层容器,
一旦引入spring-boot-starter-web模块,就默认使用tomcat。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
排除tomcat依赖;
引入其它的容器。
例如:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-undertowartifactId>
dependency>
打开spring-boot-2.2.9.RELEASE\spring-boot-project\spring-boot-autoconfigure\src\main\resources\META-INF\spring.factories,
这个ServletWebServerFactoryAutoConfiguration就是tomcat的自动配置类,
打开这个类,
EmbeddedTomcat
TomcatServletWebServerFactory,
TomcatServletWebServerFactory有一个getWebServer方法,
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
if (this.disableMBeanRegistry) {
Registry.disableRegistry();
}
// 实例化一个tomcat
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
// 设置tomcat的临时工作目录
tomcat.setBaseDir(baseDir.getAbsolutePath());
// 默认使用Http11NioProtocol实例化connector
Connector connector = new Connector(this.protocol);
connector.setThrowOnFailure(true);
// 给service添加connector
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
// 关闭热部署
tomcat.getHost().setAutoDeploy(false);
// 配置engine
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
// 实例化TomcatWebServer时会将DispatcherServlet以及一些Filter添加到tomcat
return getTomcatWebServer(tomcat);
}
实例化tomcat,
TomcatServletWebServerFactory#getTomcatWebServer
TomcatWebServer#TomcatWebServer(org.apache.catalina.startup.Tomcat, boolean)
TomcatWebServer#initialize
到this.tomcat.start();这一步,tomcat就启动了。
所以一旦TomcatServletWebServerFactory#getWebServer被调用,内嵌的tomcat就会创建并启动。
在刷新应用上下文的时候,
SpringBootMytestApplication#main
→
SpringApplication#run(java.lang.Class>, java.lang.String...) → SpringApplication#run(java.lang.Class>[], java.lang.String[])
→
SpringApplication#run(java.lang.String…)
→
SpringApplication#refreshContext
→
SpringApplication#refresh
→
AbstractApplicationContext#refresh
AbstractApplicationContext#refresh
我们关注下onRefresh();这个方法,看ServletWebServerApplicationContext的实现,
ServletWebServerApplicationContext#onRefresh
@Override
protected void onRefresh() {
super.onRefresh();
try {
// 通过Servlet容器工厂TomcatServletWebServerFactory,获取Servlet容器tomcat
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
ServletWebServerApplicationContext#createWebServer
可以看到这个类型是TomcatServletWebServerFactory,所以就是在这一步调用的。
AbstractApplicationContext#refresh
→
AbstractApplicationContext#finishRefresh
→
ServletWebServerApplicationContext#finishRefresh
@Override
protected void finishRefresh() {
super.finishRefresh();
// 实例化(在tomcat启动时就要完成实例化的Servlet:loadStartUp > 0),打印启动完成日志。
WebServer webServer = startWebServer();
if (webServer != null) {
publishEvent(new ServletWebServerInitializedEvent(webServer, this));
}
}