SpringBoot启动应用及回调监听原理解析

 更新时间:2019年12月13日 15:15:13   作者:BINGJJFLY  
这篇文章主要介绍了SpringBoot启动应用及回调监听原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了SpringBoot启动应用及回调监听原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

主类Main方法

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

创建SpringApplication对象

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

构造器

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));

执行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的实现类集合
  SpringApplicationRunListeners listeners = this.getRunListeners(args);
  // 监听开始,回调所有SpringApplicationRunListener的starting()方法
  listeners.starting();
 
  Collection exceptionReporters;
  try {
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // 监听配置环境准备好了,回调所有SpringApplicationRunListener的environmentPrepared()方法
    ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
    this.configureIgnoreBeanInfo(environment);
    Banner printedBanner = this.printBanner(environment);
    // 创建IOC容器
    context = this.createApplicationContext();
    exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
    // 回调ApplicationContextInitializer的initialize()方法,回调SpringApplicationRunListener的contextPrepared()方法
    // 回调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);
    }
    // 回调SpringApplicationRunListener的started()方法
    listeners.started(context);
    // 从IOC容器中获得ApplicationRunner、CommandLineRunner的所有组件,回调ApplicationRunner、CommandLineRunner的run()方法
    this.callRunners(context, applicationArguments);
  } catch (Throwable var10) {
    this.handleRunFailure(context, var10, exceptionReporters, listeners);
    throw new IllegalStateException(var10);
  }
 
  try {
    // 回调SpringApplicationRunListener的running()方法
    listeners.running(context);
    return context;
  } catch (Throwable var9) {
    this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
    throw new IllegalStateException(var9);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 教你如何测试Spring Data JPA的Repository

    教你如何测试Spring Data JPA的Repository

    Spring Data JPA 提供了一些便捷的方式来测试这种持久层的代码,常见的两种测试类型是集成测试和单元测试,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • Java开发工具IntelliJ IDEA安装图解

    Java开发工具IntelliJ IDEA安装图解

    这篇文章主要介绍了Java开发工具IntelliJ IDEA安装图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • java11新特性之集合转换为数组的方法

    java11新特性之集合转换为数组的方法

    Java11引入了一种将带有泛型的集合转换为带有泛型的数组的简单方法,本文通过实例代码介绍java11新特性之集合转换为数组的操作方法,感兴趣的朋友跟随小编一起看看吧
    2024-06-06
  • java使用正则表达校验手机号码示例(手机号码正则)

    java使用正则表达校验手机号码示例(手机号码正则)

    这篇文章主要介绍了java使用正则表达校验手机号码示例,可校验三个号码段:13*、15*、18*,大家根据自己的需要增加自己的号码段就可以了
    2014-03-03
  • Spring Boot集成Mybatis的实例代码(简洁版)

    Spring Boot集成Mybatis的实例代码(简洁版)

    这篇文章主要介绍了Spring Boot集成Mybatis简洁版的教程,需要的朋友可以参考下
    2018-02-02
  • Java中的线程生命周期核心概念

    Java中的线程生命周期核心概念

    这篇文章主要介绍了Java中的线程生命周期核心概念,通过使用一个快速的图解展开文章内容,需要的小伙伴可以参考一下
    2022-06-06
  • Springboot2整合knife4j过程解析

    Springboot2整合knife4j过程解析

    这篇文章主要介绍了Springboot2整合knife4j过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 使用Java快速将Web中表格转换成Excel的方法

    使用Java快速将Web中表格转换成Excel的方法

    在平时做系统项目时,经常会需要做导出功能,下面这篇文章主要给大家介绍了关于使用Java快速将Web中表格转换成Excel的相关资料,需要的朋友可以参考下
    2023-06-06
  • Java实现聊天机器人完善版

    Java实现聊天机器人完善版

    这篇文章主要为大家详细介绍了Java实现聊天机器人完善版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • Java泛型之类型擦除实例详解

    Java泛型之类型擦除实例详解

    Java泛型在使用过程有诸多的问题,如不存在List<String>.class,List<Integer>不能赋值给List<Number>(不可协变),奇怪的ClassCastException等,这篇文章主要给大家介绍了关于Java泛型之类型擦除的相关资料,需要的朋友可以参考下
    2022-01-01

最新评论