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();
    }
相关推荐
幽络源小助理3 小时前
微信小程序文章管理系统开发实现
java·微信小程序·springboot
有什么东东1 天前
山东大学软件学院创新项目实训开发日志(20)之中医知识问答自动生成对话标题bug修改
java·vue·bug·springboot
我爱布朗熊1 天前
4.RabbitMQ - 延迟消息
rabbitmq·springboot
mask哥2 天前
详解springcloudalibaba采用prometheus+grafana实现服务监控
java·nacos·springboot·grafana·prometheus·springcloud·微服务监控
幽络源小助理2 天前
SpringBoot民宿管理系统开发实现
java·spring boot·springboot·民宿系统
古渡蓝按2 天前
微信支付功能的设计实现与关键实践(UniApp+Java)全代码
springboot
Nick_zcy3 天前
协同推荐算法实现的智能商品推荐系统 - [基于springboot +vue]
算法·springboot·推荐算法
洛阳泰山4 天前
LangChain4j 搭配 Kotlin:以协程、流式交互赋能语言模型开发
java·ai·语言模型·kotlin·交互·springboot·langchain4j
飞天小牛肉6 天前
拼多多面经,暑期实习Java一面
java·spring·springboot·秋招·校招
橘猫云计算机设计6 天前
django基于爬虫的网络新闻分析系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
后端·爬虫·python·django·毕业设计·springboot