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));
		}
	}
    
相关推荐
xxwl5852 天前
数据库后端接口测试报告
spring boot·mysql·tomcat
极光代码工作室2 天前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
次次皮4 天前
Linux 下通过 XShell 手动更新 Tomcat 应用并重启
前端·tomcat·jar
周航宇JoeZhou5 天前
JP3-2-1-MyItem项目简介
java·ai·springboot·环境搭建·项目·管理·springai
剧号5 天前
互联网大厂Java面试全解析:Java SE 11, Spring Boot及微服务实战问答
java·微服务·面试·kafka·kubernetes·springboot·分布式系统
Listen·Rain5 天前
用AI开发出一个AI
java·人工智能·spring boot·tomcat·intellij-idea·mybatis·visual studio
勿忘初心12216 天前
【Windows流媒体实战2】SpringBoot2.x 整合FFmpeg 完整实操教程
ffmpeg·springboot·流媒体开发·nginx-rtmp·rtmp视频录制
Full Stack Developme6 天前
SpringBoot 内嵌 Tomcat 的启动流程
spring boot·后端·tomcat
Full Stack Developme6 天前
Tomcat 如何处理HTTP请求
java·http·tomcat
X-⃢_⃢-X7 天前
四、OpenFeign远程调用
java·微服务·springboot·springcloud