springboot啟動配置原理之二(運行run方法)


public ConfigurableApplicationContext run(String... args) {
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   FailureAnalyzers analyzers = null;
   configureHeadlessProperty();
    
   //獲取SpringApplicationRunListeners;從類路徑下META-INF/spring.factories
   SpringApplicationRunListeners listeners = getRunListeners(args);
    //回調所有的獲取SpringApplicationRunListener.starting()方法
   listeners.starting();
   try {
       //封裝命令行參數
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
      //准備環境
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
            //創建環境完成后回調SpringApplicationRunListener.environmentPrepared();表示環境准備完成
       
      Banner printedBanner = printBanner(environment);
       
       //創建ApplicationContext;決定創建web的ioc還是普通的ioc
      context = createApplicationContext();
       //異常報告
      analyzers = new FailureAnalyzers(context);
       //准備上下文環境;將environment保存到ioc中;而且applyInitializers();
       //applyInitializers():回調之前保存的所有的ApplicationContextInitializer的initialize方法
       //回調所有的SpringApplicationRunListener的contextPrepared();
       //prepareContext運行完成以后回調所有的 SpringApplicationRunListener的contextLoaded();
      prepareContext(context, environment, listeners, applicationArguments,
            printedBanner);
      
       //s刷新容器;ioc容器初始化(如果是web應用還會創建嵌入式的Tomcat);Spring注解版
       //掃描,創建,加載所有組件的地方;(配置類,組件,自動配置)
      refreshContext(context);
       //從ioc容器中獲取所有的ApplicationRunner和CommandLineRunner進行回調
       //ApplicationRunner先回調,CommandLineRunner再回調
      afterRefresh(context, applicationArguments);
 
       //所有的SpringApplicationRunListener回調finished方法
      listeners.finished(context, null);
      stopWatch.stop();
      if (this.logStartupInfo) {
         new StartupInfoLogger(this.mainApplicationClass)
               .logStarted(getApplicationLog(), stopWatch);
      }
       //整個SpringBoot應用啟動完成以后返回啟動的ioc容器;
      return context;
   }
   catch (Throwable ex) {
      handleRunFailure(context, listeners, analyzers, ex);
      throw new IllegalStateException(ex);
   }
}

//獲取SpringApplicationRunListeners;從類路徑下META-INF/spring.factories

//回調所有的獲取SpringApplicationRunListener.starting()方法

 //准備環境

//創建環境完成后回調SpringApplicationRunListener.environmentPrepared();表示環境准備完成

 //創建ApplicationContext;決定創建web的ioc還是普通的ioc

//准備上下文環境;將environment保存到ioc中;而且applyInitializers();
//applyInitializers():回調之前保存的所有的ApplicationContextInitializer的initialize方法
//回調所有的SpringApplicationRunListener的contextPrepared();
//prepareContext運行完成以后回調所有的 SpringApplicationRunListener的contextLoaded();

 
 

//從ioc容器中獲取所有的ApplicationRunner和CommandLineRunner進行回調
//ApplicationRunner先回調,CommandLineRunner再回調

 
 //所有的SpringApplicationRunListener回調finished方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM