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();
    }
相关推荐
赚钱给孩子买茅台喝18 分钟前
智能BI项目第五期
aigc·springboot·ai编程
讓丄帝愛伱6 小时前
JPA QuerySyntaxException: unexpected token: xxx near
springboot
IT学长编程2 天前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
孟诸2 天前
计算机专业毕设-校园新闻网站
java·vue·毕业设计·springboot·课程设计
_院长大人_2 天前
SpringBoot 整合docker,执行容器服务
java·docker·springboot
我是小酒3 天前
掌握 Spring:从新手到高手的常见问题汇总
java·后端·spring·springboot
赚钱给孩子买茅台喝4 天前
智能BI项目第一期
java·人工智能·springboot·react
java1234_小锋5 天前
免费分享一套SpringBoot+Vue学生信息管理系统【论文+源码+SQL脚本】,帅呆了~~
java·springboot·java毕业设计·学生信息·java学生信息·springboot学生信息·vue学生信息
蓝染-惣右介5 天前
【若依RuoYi-Vue | 项目实战】帝可得后台管理系统(一)
java·后端·物联网·vue·springboot
张某布响丸辣5 天前
Nginx 负载均衡:优化网站性能与可扩展性的利器
java·运维·nginx·负载均衡·springboot