欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2020-10-15 23:17:41|栏目:JAVA代码|点击:

这篇文章主要介绍了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);
  }
}

上一篇:Java中的字节流文件读取教程(一)

栏    目:JAVA代码

下一篇:深入理解java泛型详解

本文标题:SpringBoot启动应用及回调监听原理解析

本文地址:http://www.codeinn.net/misctech/11930.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有