SpringBoot内嵌的Tomcat:

SpringBoot内嵌Tomcat源码:

1、调用启动类SpringbootdemoApplication中的SpringApplication.run()方法。

复制代码
@SpringBootApplication
public class SpringbootdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}

2、构建完SpringApplication对象后,调用其run方法,重点关注该方法中的refreshContext(context)方法

复制代码
public ConfigurableApplicationContext run(String... args) {
		......
			refreshContext(context);
		......
		return context;
	}

private void refreshContext(ConfigurableApplicationContext context) {
		if (this.registerShutdownHook) {
			shutdownHook.registerApplicationContext(context);
		}
		refresh(context);
	}

调用SpringApplication中refreshContext()方法的refresh(context);调用ConfigurableApplicationContext 的子类ServletWebServerApplicationContext中实现的refresh()方法。

复制代码
protected void refresh(ConfigurableApplicationContext applicationContext) {
		applicationContext.refresh();
	}

ServletWebServerApplicationContext中实现的refresh()方法,该方法内部调用了其父类AbstractApplicationContext中的refresh()方法。

复制代码
@Override
	public final void refresh() throws BeansException, IllegalStateException {
		try {
			super.refresh();
		}
		catch (RuntimeException ex) {
			WebServer webServer = this.webServer;
			if (webServer != null) {
				webServer.stop();
			}
			throw ex;
		}
	}

重点关注AbstractApplicationContext中的this.onRefresh(),该方法是一个空方法,交给子类ServletWebServerApplicationContext实现,createWebServer()方法创建web服务器。

复制代码
@Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

private void createWebServer() {
        //TomcatWebServer
        WebServer webServer = this.webServer
        ServletContext servletContext = this.getServletContext();
        if (webServer == null && servletContext == null) {
            //创建WebServer步骤,并标志着他开始
            StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");
            //通过BeanName为tomcatServletWebServerFactory获取ServletWebServerFactory(TomcatServletWebServerFactory)
            ServletWebServerFactory factory = this.getWebServerFactory();
            //添加一个StartUp,标记到步骤
            createWebServer.tag("factory", factory.getClass().toString());
            //从TomcatServletWebServerFactory工厂中获得TomcatWebServer
            this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});
            //TomcatWebServer创建完成
            createWebServer.end();
	
			//在DefaultListableBeanFactory中注册单例WebServerGracefulShutdownLifecycle(Web服务器的生命周期)
        	this.getBeanFactory().registerSingleton("webServerGracefulShutdown", new WebServerGracefulShutdownLifecycle(this.webServer));
        	//在DefaultListableBeanFactory中注册单例WebServerStartStopLifecycle(Web服务器启动-停止生命周期)
            this.getBeanFactory().registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this, this.webServer));
        } else if (servletContext != null) {
            try {
                this.getSelfInitializer().onStartup(servletContext);
            } catch (ServletException var5) {
                throw new ApplicationContextException("Cannot initialize servlet context", var5);
            }
        }
        //初始化Servlet相关属性源(获取相关环境配置ConfigurableEnvironment)
        this.initPropertySources();
    }
相关推荐
bingbingyihao2 天前
多数据源 Demo
java·springboot
TinpeaV8 天前
(JAVA)自建应用调用企业微信API接口,实现消息推送
java·redis·企业微信·springboot·springflux
tanxiaomi10 天前
学习分库分表的前置知识:高可用系统架构理论与实践
java·mysql·spring cloud·系统架构·springboot
麦兜*17 天前
国产大模型平替方案:Spring Boot通义千问API集成指南
java·spring boot·后端·python·spring cloud·系统架构·springboot
core51217 天前
fastdfs快速部署、集成、调优
docker·部署·springboot·fastdfs·调用
尚学教辅学习资料17 天前
SpringBoot3.x入门到精通系列: 2.3 Web开发基础
前端·springboot·web开发
97zz17 天前
项目配置文件正确但是启动失败,报配置文件内容错误或中间件地址与实际不符
java·中间件·springboot
闫小甲17 天前
jobrunr xxljob 怎么选?
springboot·xxljob·jobrunr
core51217 天前
elk快速部署、集成、调优
elk·springboot·kibana·索引·查询
还是鼠鼠18 天前
tlias智能学习辅助系统--SpringAOP-进阶-通知顺序
java·后端·mysql·spring·mybatis·springboot