spring-boot啟動

辉辉、發表於2024-08-11
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    //建立引導上下文
    DefaultBootstrapContext bootstrapContext = createBootstrapContext();
    ConfigurableApplicationContext context = null;
    configureHeadlessProperty();
    
    //生成啟動監視器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    
    //resources下建立META-INF/spring.factories檔案
    //載入監聽器如下方
    //org.springframework.boot.SpringApplicationRunListener=\
    //org.springframework.boot.context.event.EventPublishingRunListener
    listeners.starting(bootstrapContext, this.mainApplicationClass);
    try {
       //初始化應用引數類
       ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        
       //準備環境引數
       ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
       configureIgnoreBeanInfo(environment);
        
       //列印影像控制檯
       Banner printedBanner = printBanner(environment);
        
       //建立spring上下文
       context = createApplicationContext();
       context.setApplicationStartup(this.applicationStartup);
        
       //準備上下文
       //完成事件ApplicationContextInitializedEvent
       //完成事件ApplicationPreparedEvent
       prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
        
       //重新整理上下文
       //上下文重新整理完成事件ContextRefreshedEvent
       //Web伺服器完成事件WebServerlnitializedEvent
       refreshContext(context);
        
       afterRefresh(context, applicationArguments);
       stopWatch.stop();
       if (this.logStartupInfo) {
          new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
       }
       listeners.started(context);
       callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
       handleRunFailure(context, ex, listeners);
       throw new IllegalStateException(ex);
    }

    try {
       listeners.running(context);
    }
    catch (Throwable ex) {
       handleRunFailure(context, ex, null);
       throw new IllegalStateException(ex);
    }
    return context;
}

相關文章