溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot啟動應(yīng)用及回調(diào)監(jiān)聽原理解析

發(fā)布時間:2020-10-25 02:09:15 來源:腳本之家 閱讀:198 作者:BINGJJFLY 欄目:編程語言

這篇文章主要介紹了SpringBoot啟動應(yīng)用及回調(diào)監(jiān)聽原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

主類Main方法

public static void main(String[] args) {
  SpringApplication.run(SpringBootRunApplication.class, args);
}

創(chuàng)建SpringApplication對象

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

構(gòu)造器

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  this.sources = new LinkedHashSet();
  this.bannerMode = Mode.CONSOLE;
  this.logStartupInfo = true;
  this.addCommandLineProperties = true;
  this.addConversionService = true;
  this.headless = true;
  this.registerShutdownHook = true;
  this.additionalProfiles = new HashSet();
  this.isCustomEnvironment = false;
  this.lazyInitialization = false;
  this.resourceLoader = resourceLoader;
  Assert.notNull(primarySources, "PrimarySources must not be null");
  this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
  this.webApplicationType = WebApplicationType.deduceFromClasspath();
  this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
  this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
  this.mainApplicationClass = this.deduceMainApplicationClass();
}

ApplicationContextInitializer&ApplicationListener

讀取META-INF/spring.factories文件中的類 

this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));

執(zhí)行run方法 

public ConfigurableApplicationContext run(String... args) {
  StopWatch stopWatch = new StopWatch();
  stopWatch.start();
  // IOC容器
  ConfigurableApplicationContext context = null;
  Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
  this.configureHeadlessProperty();
  // 讀取META-INF/spring.factories文件獲得SpringApplicationRunListener的實現(xiàn)類集合
  SpringApplicationRunListeners listeners = this.getRunListeners(args);
  // 監(jiān)聽開始,回調(diào)所有SpringApplicationRunListener的starting()方法
  listeners.starting();
 
  Collection exceptionReporters;
  try {
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // 監(jiān)聽配置環(huán)境準(zhǔn)備好了,回調(diào)所有SpringApplicationRunListener的environmentPrepared()方法
    ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
    this.configureIgnoreBeanInfo(environment);
    Banner printedBanner = this.printBanner(environment);
    // 創(chuàng)建IOC容器
    context = this.createApplicationContext();
    exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
    // 回調(diào)ApplicationContextInitializer的initialize()方法,回調(diào)SpringApplicationRunListener的contextPrepared()方法
    // 回調(diào)SpringApplicationRunListener的contextLoaded()方法
    this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    // 刷新IOC容器,注入組件
    this.refreshContext(context);
    this.afterRefresh(context, applicationArguments);
    stopWatch.stop();
    if (this.logStartupInfo) {
      (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
    }
    // 回調(diào)SpringApplicationRunListener的started()方法
    listeners.started(context);
    // 從IOC容器中獲得ApplicationRunner、CommandLineRunner的所有組件,回調(diào)ApplicationRunner、CommandLineRunner的run()方法
    this.callRunners(context, applicationArguments);
  } catch (Throwable var10) {
    this.handleRunFailure(context, var10, exceptionReporters, listeners);
    throw new IllegalStateException(var10);
  }
 
  try {
    // 回調(diào)SpringApplicationRunListener的running()方法
    listeners.running(context);
    return context;
  } catch (Throwable var9) {
    this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
    throw new IllegalStateException(var9);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI