springboot内置Tomcat流程

1、org.springframework.boot.SpringApplication#initialize

java 复制代码
setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));

加载了org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext

2、spring refresh 中onRefresh回调中启动了内置Tomcat

ServletWebServerApplicationContext继承AbstractApplicationContext,并且重新实现了onRefresh方法。

java 复制代码
	//重写了refresh方法,启动
    @Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

    //启动Tomcat
	private void createWebServer() {
		WebServer webServer = this.webServer;
		ServletContext servletContext = getServletContext();
		if (webServer == null && servletContext == null) {
			ServletWebServerFactory factory = getWebServerFactory();
			this.webServer = factory.getWebServer(getSelfInitializer());
		}
		else if (servletContext != null) {
			try {
				getSelfInitializer().onStartup(servletContext);
			}
			catch (ServletException ex) {
				throw new ApplicationContextException("Cannot initialize servlet context", ex);
			}
		}
		initPropertySources();
	}

    //如果Tomcat启动了,就发布服务ServletWebServerInitializedEvent
	@Override
	protected void finishRefresh() {
		super.finishRefresh();
		WebServer webServer = startWebServer();
		if (webServer != null) {
			publishEvent(new ServletWebServerInitializedEvent(webServer, this));
		}
	}
    
相关推荐
洛阳泰山11 小时前
Maxkb4j集成sqlbot MCP实现企业智能问数智能体
java·ai·springboot·agent·智能问数
0和1的搬运工12 小时前
基于Java+SpringBoot+Vue+HTML5高校教师电子名片系统(源码+LW+调试文档+讲解等)/高校教师/电子名片/系统/教育科技/教育信息化/名片管理/电子身份/教师信息管理/校园信息化
spring cloud·tomcat·log4j·maven·intellij-idea·dubbo·java-consul
铁皮哥15 小时前
【后端开发】@Resource 和 @Autowired 到底有什么区别?为什么现在更推荐构造方法注入?
java·ide·spring boot·tomcat·log4j·idea·intellij idea
天码-行空17 小时前
深入拆解 Jetty & Tomcat:Connector 架构差异与设计精髓
架构·tomcat·jetty
zc.z1 天前
基于 LangChain4j 的 RAG 工作流智能体实战
langchain·大模型·springboot·rag智能体
suweijie7682 天前
Nacos配置读取异常排查与解决指南
微服务·nacos·springboot·配置中心·问题排查
程序员老邢2 天前
【产品底稿 12】工程架构最终定型:完整模块拆分、分包规范、层级依赖与开发规约全清单
微服务·架构·springboot·多模块·技术债务
abcnull3 天前
Springboot+Vue2的Web项目小白入门Demo快速学习!
java·elementui·vue·maven·springboot·web·小白
G.晴天3 天前
Linux常用命令练习流程
java·linux·运维·服务器·tomcat
seven97_top3 天前
Tomcat 线程池的设计与实现:StandardThreadExecutor
java·tomcat