1 springApplication的run
  run方法主要是用于创造spring容器ConfigurableApplicationContext对象。

public ConfigurableApplicationContext run(String... args) {
  StopWatch stopWatch = new StopWatch(); // 构造一个任务执行观察器
  stopWatch.start(); // 开始执行,记录开始时间
  ConfigurableApplicationContext context = null;
  configureHeadlessProperty();
  // 获取SpringApplicationRunListeners,内部只有一个EventPublishingRunListener
  SpringApplicationRunListeners listeners = getRunListeners(args);
  // 会封装成SpringApplicationEvent事件然后广播出去给SpringApplication中的listeners所监听
  // 这里接受ApplicationStartedEvent事件的listener会执行相应的操作
  listeners.started();
  try {
  // 构造一个应用程序参数持有类
  ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

  //创建并配置环境

  ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
  //输出banner

Banner printedBanner = printBanner(environment);
  context = createApplicationContext();
  analyzers = new FailureAnalyzers(context);
  prepareContext(context, environment, listeners, applicationArguments,printedBanner);
  refreshContext(context);
  afterRefresh(context, applicationArguments);
  listeners.finished(context, null);
  stopWatch.stop();
  if (this.logStartupInfo) {
  new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
  }
  return context;

  }
  catch (Throwable ex) {
  handleRunFailure(context, listeners, ex); // 这个过程报错的话会执行一些异常操作、然后广播出ApplicationFailedEvent事件给相应的监听器执行
  throw new IllegalStateException(ex);
  }
}

2 run的过程

按顺序看run,发现执行了下面这些过程。

(1 )stopWatch 记录开始结束时间,configureHeadlessProperty 设置headless模式,awt的headless,一般运行在服务器端,在没有显示器和鼠标键盘的模式下工作,模拟输入输出设备功能。

(2)springApplicationListeners: 这个类包含了SpringApplicationRunlistener的集合和一个日志类。SpringApplicationRunlistener也是通过spring.factories加载,如下可以看到,就包含一种EventPublishingRunListener。
  org.springframework.boot.SpringApplicationRunListener=\
  org.springframework.boot.context.event.EventPublishingRunListener
  EventPublishingRunListener通过看源码,发现包含了started environmentPrepared contextPrepared contextLoaded等方法,基本都是包装特定event并发event。配合run函数的代码如下,可以得知,springApplicationListeners主要负责在run函数的各个阶段,发起事件。比如在创建spring容器的前后调用了start和finish方法,在创建容器时,把listener当参数传入便于在各个时间发起事件。

(3)prepareEnvironment  创建并设置环境

  private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments) {
  // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    listeners.environmentPrepared(environment);
    if (isWebEnvironment(environment) && !this.webEnvironment) {
      environment = convertToStandardEnvironment(environment);
    }
    return environment;
  }

如上,设置环境分几个步骤,getOrderCreateEnvironment获得一个标准环境对象StandardEnvironment,然后configureEnvironment进行配置。其中configureEnvironment 包含两个配置,configurePropertySources和configureProfiles两个方法。Environment代表程序运行环境,包含两种信息,一个是profiles,描述各种bean defination,另一种是properties描述系统配置,包括配置文件 jvm属性文件 环境变量等。configureEnvironment方法先是调用configurePropertySources来配置properties,然后调用configureProfiles来配置profiles。然后listener发出事件。

(4)pringBnner 打印banner,打印banner信息,即启动时出现的logo信息。

(5)createApplicationContext  创建spring容器。如果是web程序,那么构造org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext容器,否则构造org.springframework.context.annotation.AnnotationConfigApplicationContext容器

(6)prepareContext    首先调用了postProcessApplicationContext(context),如果是web程序并且是spring web容器,那么设置实例命名生成器并注册,同时注册资源加载器并注册。然后调用applyInitializers(context),遍历每个初始化器,调用相应的innitialize方法让初始化器开始工作。

(7)refreshContext(context) 刷新spring容器,内部做很多事情,包括springFactory设置,BeanFactoryPostProcessor接口执行,BeanPostProcessor接口执行,自动化配置类解析,条件注解解析,国际化的初始化等。

(8)afterRefresh  调用了 callRunners,主要是得到所有的CommonandLineRunner和ApplicationRunner, 使用 @Order(value=x)设置排序值,可以支持自定义runner完成自己需求。

private void callRunners(ApplicationContext context, ApplicationArguments args) {
  List<Object> runners = new ArrayList<Object>();
  // 找出Spring容器中ApplicationRunner接口的实现类
  runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
  // 找出Spring容器中CommandLineRunner接口的实现类
  runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
  // 对runners进行排序
  AnnotationAwareOrderComparator.sort(runners);
  // 遍历runners依次执行
      for (Object runner : new LinkedHashSet<Object>(runners)) {
    if (runner instanceof ApplicationRunner) { // 如果是ApplicationRunner,进行ApplicationRunner的run方法调用
      callRunner((ApplicationRunner) runner, args);
    }
  if (runner instanceof CommandLineRunner) { // 如果是CommandLineRunner,进行CommandLineRunner的run方法调用
      callRunner((CommandLineRunner) runner, args);
    }
  }
}

springboot启动过程(2)-run方法的更多相关文章

  1. SpringBoot启动过程原理

    最近这两年springboot突然火起来了,那么我们就来看看springboot的运行原理. 一.springboot的三种启动方式: 1.运行带有main方法的2.通过命令 Java -jar命令3 ...

  2. Spring Boot 学习笔记一(SpringBoot启动过程)

    SpringBoot启动 Spring Boot通常有一个名为*Application的入口类,在入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法. 在main方法 ...

  3. (四)SpringBoot启动过程的分析-预处理ApplicationContext

    -- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇(三)SpringBoot启动过程的分析-创建应用程序上下文,本文将分析上下文创建完毕之后的下一步操作:预处理上下文容器. 预处理上下文 ...

  4. (一)SpringBoot启动过程的分析-启动流程概览

    -- 以下内容均基于2.1.8.RELEASE版本 通过粗粒度的分析SpringBoot启动过程中执行的主要操作,可以很容易划分它的大流程,每个流程只关注重要操作为后续深入学习建立一个大纲. 官方示例 ...

  5. (五)SpringBoot启动过程的分析-刷新ApplicationContext

    -- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇[(四)SpringBoot启动过程的分析-预处理ApplicationContext] (https://www.cnblogs.co ...

  6. (三)SpringBoot启动过程的分析-创建应用程序上下文

    -- 以下内容均基于2.1.8.RELEASE版本 紧接着上一篇(二)SpringBoot启动过程的分析-环境信息准备,本文将分析环境准备完毕之后的下一步操作:ApplicationContext的创 ...

  7. springboot启动过程(3)-refresh方法

    1  springboot在启动的时候,会调用run方法,创建环境设置spring容器,其中包含refresh方法,完成配置类解析,各种beanFactoryPostProcess和beanPostP ...

  8. springboot启动过程(1)-初始化

    1   springboot启动时,只需要调用一个类前面加了@SpringBootApplication的main函数,执行SpringApplication.run(DemoApplication. ...

  9. SpringBoot启动过程原理(转)

    1.1 Springboot启动: @SpringBootApplication public class ServerApplication { public static void main(St ...

随机推荐

  1. 深入javascript之原型和原型链

    原型和原型链是js中的难点也是重点,明白了原型和原型链会让我们在后面不管是学习还是工作都会更加高效,并且原型和原型链会是面试中必不可少的话题.看完此篇文章一定会让你对原型,原型链有深刻全面的了解. 一 ...

  2. JS开发页面小组件:table组件

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 机器学习敲门砖:任何人都能看懂的TensorFlow介绍

    机器学习敲门砖:任何人都能看懂的TensorFlow介绍 http://www.jiqizhixin.com/article/1440

  4. ThinkPHP中的find和select的区别

    ThinkPHP作为PHP中应用广泛又好用的框架,能比较快速的开发MVC架构的管理系统,获得了大量的应用.但是在ThinkPHP中select()和find()方法有什么区别呢? 事实上find()返 ...

  5. UVA - 1331 Minimax Triangulation (区间dp)(最优三角剖分)

    题目链接 把一个多边形剖分成若干个三角形,使得其中最大的三角形面积最小. 比较经典的一道dp问题 设dp[l][r]为把多边形[l,r]剖分成三角形的最大三角形面积中的最小值,则$dp[l][r]=m ...

  6. Why getting this error “django.db.utils.OperationalError: (1050, ”Table 'someTable' already exists“)”

    0down votefavorite   I am getting error like django.db.utils.OperationalError: (1050, "Table 's ...

  7. JSONP解决跨域方案

    一.jsonp原理 本质并不是ajax,只是执行了跨域js,所以该方式只支持get方式 html中,所有带src属性的标签都可以跨域script img iframe 所以,可以通过script加载其 ...

  8. SQL Sever 学习系列之三

    SQL Sever 学习系列之三 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题)         五.经理今天刚谈到with的用法(with的 ...

  9. Eureka详解

    该章节紧接第三章,主要是对一些理论上的指导,帮助更好的理解服务治理. 基础架构 eureka服务治理的基础架构包含三个要素: 1)服务注册中心:eureka提供服务端,提供服务的注册与发现功能. 注: ...

  10. Spring基础知识之装配Bean

    装配(wiring):创建应用对象之间协作关系的行为.这是依赖注入的本质. Spring配置的可选方案 Spring提供了三种装配机智: 1)在XML中进行显示装配 2)在java中进行显示装配 3) ...