springboot版本:2.2.9.RELEASE。
默认Servlet容器
springboot默认支持tomcat、jetty、undertow作为底层容器,
一旦引入spring-boot-starter-web模块,就默认使用tomcat。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
切换Servlet容器
排除tomcat依赖;
引入其它的容器。
例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
内嵌tomcat自动配置原理
tomcat自动配置类
打开spring-boot-2.2.9.RELEASE\spring-boot-project\spring-boot-autoconfigure\src\main\resources\META-INF\spring.factories,
这个ServletWebServerFactoryAutoConfiguration就是tomcat的自动配置类,
打开这个类,
EmbeddedTomcat
tomcat工厂类
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()
我们关注下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,所以就是在这一步调用的。
finishRefresh()
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));
}
}
到此这篇关于Springboot内嵌tomcat应用原理深入分析的文章就介绍到这了,更多相关Springboot内嵌tomcat内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Springboot内嵌tomcat应用原理深入分析


- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- JSP 制作验证码的实例详解 2023-07-30
- Java实现顺序表的操作详解 2023-05-19
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- 深入了解Spring的事务传播机制 2023-06-02
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Java中的日期时间处理及格式化处理 2023-04-18
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- JSP页面间传值问题实例简析 2023-08-03