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();
    }
相关推荐
写不出来就跑路2 天前
SpringBoot静态资源与缓存配置全解析
java·开发语言·spring boot·spring·springboot
lingRJ7774 天前
从混沌到掌控:基于OpenTelemetry与Prometheus构建分布式调用链监控告警体系
java·springboot·prometheus·backend·opentelemetry·jaeger·microservices
endswel4 天前
Spring MVC HandlerInterceptor 拦截请求及响应体
springmvc·springboot
保持学习ing6 天前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储
默默coding的程序猿7 天前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
衍生星球8 天前
Maven 3.9.6的下载和配置
java·maven·springboot
还是鼠鼠19 天前
日志技术-Logback入门程序
java·后端·spring·springboot·logback
starstarzz21 天前
解决idea无法正常加载lombok包
java·ide·spring·intellij-idea·springboot·web
vivo互联网技术23 天前
Spring Boot 启动优化实践
后端·springboot·spring启动加速·异步初始化
七烦25 天前
vue3 + springboot实现微信登录
vue·springboot·微信公众号