• springboot中内置tomcat什么时候创建的,又是什么时候启动的?


    与spring和springboot相关的重要逻辑,如果想了解源头在哪,找refresh准没错

    启动类,run

    1. @SpringBootApplication
    2. public class KafkaBootApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(KafkaBootApplication.class, args);
    5. }
    6. }

    一路run

    1. public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    2. return run(new Class<?>[] { primarySource }, args);
    3. }
    1. public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    2. return new SpringApplication(primarySources).run(args);
    3. }

    直到refreshContext(context)

     

    1. private void refreshContext(ConfigurableApplicationContext context) {
    2. if (this.registerShutdownHook) {
    3. shutdownHook.registerApplicationContext(context);
    4. }
    5. refresh(context);
    6. }
    1. protected void refresh(ConfigurableApplicationContext applicationContext) {
    2. applicationContext.refresh();
    3. }

     走到ServletWebServerApplicationContext#refresh()
    1. @Override
    2. public final void refresh() throws BeansException, IllegalStateException {
    3. try {
    4. super.refresh();
    5. }
    6. catch (RuntimeException ex) {
    7. WebServer webServer = this.webServer;
    8. if (webServer != null) {
    9. webServer.stop();
    10. }
    11. throw ex;
    12. }
    13. }

    其实调用还是super.refresh(),也就是AbstractApplicationContext的refresh,再调用onRefresh()

    然后又回调回了ServletWebServerApplicationContext#onRefresh()

    这里涉及到一个设计模式,模板方法设计模式,父类的onRefresh()是个空方法,留给子类实现。

     

    内置的tomcat就是在createWebServer()方法中

     直接显示的new了一个Tomcat()

    创建的只是一个Tomcat对象,需要返回的是 TomcatWebServer,也就是启动了的Tomcat

    所以还需要启动Tomcat

     把Tomcat当作参数传过来,实例化一个TomcatWebServer对象

    实例化过程中有个步骤叫初始化initialize()

     

     就是在这个初始化方法中完成了Tomcat的启动

     至此,springboot内置的tomcat就创建并启动完成了

  • 相关阅读:
    移植RTOS的大体思路
    vue父子页面传值问题
    第七章TCP/IP——ARP网络攻击与欺骗
    【SpringBoot集成Redis + Session持久化存储到Redis】
    项目管理软件dhtmlxGantt配置教程(十六):如何设置动态化比例
    Android12版本闹钟服务崩溃问题
    Java+SpringBoot+Vue:瑜伽馆管理的黄金组合
    猿创征文|【电源专题】案例:怎么用万用表测试静态电流IQ
    win10系统下使用opencv-dnn部署yolov5模型
    Node.js 入门教程 1 Node.js 简介
  • 原文地址:https://blog.csdn.net/leisure_life/article/details/125513327