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));
		}
	}
    
相关推荐
STUPID MAN8 小时前
Linux使用tomcat发布vue打包的dist或html
linux·vue.js·tomcat·html
Kent_J_Truman18 小时前
JDK Maven Tomcat Spring在VSCode中的部分配置细节(自用)
java·tomcat·maven
心随雨下19 小时前
Tomcat日志配置与优化指南
java·服务器·tomcat
心随雨下2 天前
Java中将System.out内容写入Tomcat日志
java·开发语言·tomcat
刘一说3 天前
深入解析 Spring Boot 数据访问:Spring Data JPA 与 MyBatis 集成实战
spring boot·tomcat·mybatis
NON-JUDGMENTAL3 天前
Tomcat 新手避坑指南:环境配置 + 启动问题 + 乱码解决全流程
java·tomcat
悟能不能悟4 天前
idea运行tomcat的日志文件放到哪里了
java·tomcat·intellij-idea
任风雨4 天前
13.2.3.Tomcat
java·tomcat
邂逅星河浪漫4 天前
【RabbitMQ】消息队列·详解+实操演示+功能实现(微服务架构)
rabbitmq·springboot·springamqp·consumer·exchange·producer·queue
百锦再4 天前
第2章 第一个Rust程序
java·开发语言·后端·rust·eclipse·tomcat·hibernate