SpringBoot中内嵌的tomcat启动过程

SpringBoot启动tomcat

SpringApplication
构造器初始化时会选择webApplicationType在run方法里面获取具体的appplicationContext

ApplicationContext,

SpringBoot 初始化功能
  1. SpringApplication.run()方法解析
SpringApplication.run()
    
public ConfigurableApplicationContext run(String... args) {
  // .. 省略前边代码
  
    // 创建ApplicationContext
   context = createApplicationContext();
  // .. 省略后边代码
    
 
}

protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            switch (this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
                    break;
                case REACTIVE:
                    contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
                    break;
                default:
                    contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
            }
        }
        catch (ClassNotFoundException ex) {
            throw new IllegalStateException(
                "Unable create a default ApplicationContext, "
                + "please specify an ApplicationContextClass",
                ex);
        }
    }
    return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
  1. Spring容器启动过程后会调用onR
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh

protected void onRefresh() {
    super.onRefresh();
    try {
        createWebServer();
    }
    catch (Throwable ex) {
        throw new ApplicationContextException("Unable to start web server", ex);
    }
}

createWebServer(){
    
    
}
//ServletWebServerApplicationContext#onRefresh()>>TomcatServletWebServerFactory.getWebServer()
//>>getWebServer()>> getTomcatWebServer(tomcat)>>new TomcatWebServer()

public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
    Assert.notNull(tomcat, "Tomcat Server must not be null");
    this.tomcat = tomcat;
    this.autoStart = autoStart;
    initialize();
}

private void initialize() throws WebServerException {
    TomcatWebServer.logger
        .info("Tomcat initialized with port(s): " + getPortsDescription(false));
    synchronized (this.monitor) {
        try {
         
     
            // Start the server to trigger initialization listeners
            this.tomcat.start();

        }
        catch (Exception ex) {
            stopSilently();
            throw new WebServerException("Unable to start embedded Tomcat", ex);
        }
    }
}

  1. Spring会自动装配几个类

    tomcatServletWebServerFactoryCustomizer;里面有注册后置处理器,后置处理器会调用ServerFactoryCustomizer.customize()

    容器默认注入了2个ServerFactoryCustomizer的子类分别是

    • TomcatWebServerFactoryCustomizer
      在这里插入图片描述

    • TomcatServletWebServerFactoryCustomizer

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值