当前位置: 首页 > news >正文

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()方法。

@Overridepublic 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服务器。

@Overrideprotected void onRefresh() {super.onRefresh();try {createWebServer();}catch (Throwable ex) {throw new ApplicationContextException("Unable to start web server", ex);}}
private void createWebServer() {//TomcatWebServerWebServer webServer = this.webServerServletContext 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工厂中获得TomcatWebServerthis.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();}

在这里插入图片描述

http://www.lryc.cn/news/101512.html

相关文章:

  • 企业级docker应用注意事项
  • 腾讯云高性能计算集群CPU服务器处理器说明
  • tinkerCAD案例:23.Tinkercad 中的自定义字体
  • Box-Cox 变换
  • Linux wc命令用于统计文件的行数,字符数,字节数
  • Python读取多个栅格文件并提取像元的各波段时间序列数据与变化值
  • Linux 之 wget curl
  • AngularJS 和 React区别
  • 【Solr】Solr搜索引擎使用
  • 一起学算法(选择排序篇)
  • 智能体的主观和能动
  • AB 压力测试
  • 多旋翼物流无人机节能轨迹规划(Python代码实现)
  • Vue通过指令 命令将打包好的dist静态文件上传到腾讯云存储桶 (保存原有存储目录结构)
  • Linux 新硬盘分区,挂载
  • Stable Diffusion 开源模型 SDXL 1.0 发布
  • NoSQL--------- Redis配置与优化
  • Ubuntu中关闭防火墙
  • java-马踏棋盘
  • 系统架构设计师-软件架构设计(4)
  • 51单片机--AD/DA
  • 网络安全-防御需知
  • C#百万数据处理
  • windows端口占用
  • 如何理解Diffusion
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[聊天模型(Chat Models):使用少量示例和响应流式传输]
  • Java在线OJ项目(三)、前后端交互API模块
  • 项目——负载均衡在线OJ
  • idea连接远程服务器上传war包文件
  • 使用PyGWalker可视化分析表格型数据