一文搞懂SpringBoot内嵌的Tomcat:

SpringBoot源码系列:
一文搞懂Spring Boot中java -jar启动jar包的原理
一文搞懂SpringBoot启动流程及自动配置
一文搞懂SpringBoot内嵌的Tomcat
一文搞懂SpringApplication对象的构建及spring.factories的加载时机
一文搞懂Spring Boot 事件监听机制

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();
    }

整体流程梳理:
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值