springboot 加载 tomcat 源码追踪

加载 TomcatServletWebServerFactory

从 SpringApplication.run()方法进入

进入到 refresh () 方法

选择实现类 ServletWebServerApplicationContext

进入到 AbstractApplicationContext

onRefresh() 方法创建容器

找到加载bean

得到 webServer 实例

点击 getWebServer()

其实现类

如何知到是哪个实现类呢?接着往下看

自动装配 TomcatServletWebServerFactory

在 spring-boot-autoconfigure 包下找到 spring-autoconfigure-metadata.properties 文件

搜索 javax.servlet.ServletRequest 找到

复制代码
ServletWebServerFactoryAutoConfiguration 

核心就在 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration

这个配置类中

点击 EmbeddedTomcat.class 进入 ServletWebServerFactoryConfiguration

该类根据项目中是否含有指定的类来加载对应的容器,并创建 ServletWebServerFactory

tomcat

jetty

Undertow

当 摁下 ctrl 可以跳转,则整明该项目下有这个类,

根据 @ConditionalOnClass 注解 则可以加载 EmbeddedTomcat

否则不能加载

项目因引入该依赖,从而引入了 tomcat 容器

至此, TomcatServletWebServerFactory 随着项目启动加载到了 spring 管理的 bean 当中

上文便可以获取到该工厂,初始化 tomcat 容器。

相关推荐
昵称为空C1 分钟前
SpringBoot基于注解的数据库字段回填方案
spring boot·spring
shayudiandian21 分钟前
【Java】面向对象编程
java
optimistic_chen34 分钟前
【Java EE进阶 --- SpringBoot】Spring 核心 --- AOP
spring boot·笔记·spring·java-ee·aop·java注解
asom2234 分钟前
互联网大厂Java求职面试实战:Spring Boot到Kubernetes的技术问答
java·spring boot·kubernetes·oauth2·电商·microservices·面试技巧
I_Jln1 小时前
CountDownLatch:让多线程同步如此简单
java
虎子_layor1 小时前
轻量级哈希扰动工具:Hashids,快速上手
java·spring
逻极1 小时前
VS Code之Java 开发完全指南:从环境搭建到实战优化
java·开发语言
Moe4881 小时前
JDK动态代理和CGLIB动态代理源码解析
java·后端
月月玩代码1 小时前
SLF4J,简单门面Java日志框架
java·开发语言
虎子_layor1 小时前
号段模式(分布式ID)上手指南:从原理到实战
java·后端