SpringBoot源碼解析-啓動流程(二)

上一節,咱們分析了springboot啓動流程中SpringApplication 類的新建過程。知道了其在新建過程當中導入了幾個Initializer和Listener,這一節分析一下run方法的執行邏輯。java

public ConfigurableApplicationContext run(String... args) {
   //stopwatch主要是一個監控工具,記錄啓動的耗時。
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
   //配置java的headless模式
   configureHeadlessProperty();
   //獲取linstener並啓動他們
   SpringApplicationRunListeners listeners = getRunListeners(args);
   listeners.starting();
   try {
      //記錄啓動時輸入的參數
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
      //準備環境相關配置
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
      configureIgnoreBeanInfo(environment);
      //打印啓動標誌
      Banner printedBanner = printBanner(environment);
      //建立applicationContext
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(
            SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
      //準備而且刷新applicationContext
      prepareContext(context, environment, listeners, applicationArguments,
            printedBanner);
      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, exceptionReporters, listeners);
      throw new IllegalStateException(ex);
   }

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

配合註釋,開始分析run方法的邏輯。spring

StopWatch主要起一個監控的做用,裏面的邏輯並不複雜。後面配置了java的headless模式,這個模式和咱們的主流程邏輯影響不大,讀者能夠自行了解一下。tomcat

在下一行,就到了獲取listeners並調用其start方法。這個地方通常人確定會覺得springboot會直接獲取在新建時已經獲得的listener類。可是 springboot並無這樣作,這個地方就體現了我一開始說的,有時候咱們感受很簡單的邏輯,springboot想的比咱們更多,因此作的也更復雜。因此關於listener類的調用咱們先不深刻分析,後面結合配置的加載一併分析,在這兒,咱們只須要知道調用了listener類的start方法便可。springboot

在後面一行,使用ApplicationArguments 記錄了啓動時的參數,而且使用參數配置了環境類environment。environment主要的做用就是記錄了咱們在配置文件中配置的那些鍵值對。因此通過這步以後咱們的配置就已經被讀取到項目中了。bash

printBanner方法主要的做用就是打印了啓動時的標誌。服務器

private Banner printBanner(ConfigurableEnvironment environment) {
   if (this.bannerMode == Banner.Mode.OFF) {
      return null;
   }
   ResourceLoader resourceLoader = (this.resourceLoader != null)
         ? this.resourceLoader : new DefaultResourceLoader(getClassLoader());
   SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
         resourceLoader, this.banner);
   if (this.bannerMode == Mode.LOG) {
      return bannerPrinter.print(environment, this.mainApplicationClass, logger);
   }
   //進入打印方法
   return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}

public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
   //獲取須要打印的方案
   Banner banner = getBanner(environment);
   banner.printBanner(environment, sourceClass, out);
   return new PrintedBanner(banner, sourceClass);
}複製代碼

在getBanner方法中,咱們能夠看到springboot是先看看用戶有沒有自定義,若是有就打印用戶自定義的,若是沒有就打印默認的,默認就是咱們經常看到的spring那個圖案。app

若是須要修改的化,咱們能夠直接在resources文件夾下面新建banner.txt文件,裏面放置咱們想要的圖案好比:框架

****************************************
*         *                 *          *
*      *      *           *     *      *
*   *            *     *           *   *
*                   *                  *
* *                                  * *
*                                      *
*    *                             *   *
*       *                       *      *
*          *                 *         *
*             *           *            *
*                *     *               *
*                   *                  *
****************************************複製代碼

這樣咱們就能夠在控制檯看到改變了。less

再往下,則開始了對applicationContext類的建立及準備操做。若是讀過spring源碼的話應該知道對spring框架來講這是一個極其重要的類。這個類啓動完成,就表明spring容器的建立完成了。在springboot中applicationContext還負責了內嵌服務器(好比tomcat之類)的啓動工做。因此這部分邏輯,等到後面類加載過程以及內嵌tomcat啓動分析的時候,在詳細深刻分析。工具

因此到這兒,從頂層看springboot的邏輯已經結束了。這就是我一開始說的,從頂層看springboot的邏輯其實並不複雜。頂層邏輯看完以後,咱們就要開始分析重點了。這個時候挑戰纔剛剛開始。

返回目錄

相關文章
相關標籤/搜索