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));
		}
	}
    
相关推荐
山甫aa2 天前
Javaweb---MyBatis增删改查
java·数据库·后端·mybatis·springboot
竹枝溪2 天前
Tomcat与Servlet全套小白教程
java·servlet·tomcat·cookie·filter·session·listener
choumou_M2 天前
SpringBoot_2:MyBatis-Generator插件使用教程
spring boot·tomcat·mybatis
山甫aa2 天前
JavaWeb后端开发学习手册
java·开发语言·数据库·学习·mysql·springboot·web
小博测试成长之路3 天前
55分钟能用飞算JavaAI搭完JWT REST API吗?
springboot·ai编程·java开发·飞算javaai·ai coding·java代码生成
努力就够了3 天前
搭建属于自己的 AI 智能体以及多 Agent 协作
springboot·ai agent·ai 智能体·多 agent 协作
chaser&upper4 天前
飞算JavaAI能听懂食品安全召回系统的复杂业务吗?
springboot·crud·vibe coding·飞算javaai·ai coding模型
承渊政道4 天前
10:30提交预约会不会撞上10:00的会议?我用飞算JavaAI3.9.1和3.9.9跑了四个时间段
java·springboot·ai编程·飞算javaai·java代码生成
坐吃山猪4 天前
WebClient内存配置解析
开发语言·springboot·webflux
Pocker_Spades_A5 天前
飞算JavaAI 48分钟能做完Java全栈项目吗?
java·springboot·#ai编程·#飞算javaai·#java代码生成·#aicoding模型·#java开发