SpringBoot版本:2.1.2.RELEASE

1.maven

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>   <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.主程序入口,两种方式

  •   SpringApplication.run(Application.class);
  •   new SpringApplication(Application.class).run(args);可以通过设置springApplication的defaultProperties等属性,设置一些配置参数信息

2.1. 初始化springApplication对象

    

    listeners.starting();    EventPublishingRunListener

    发布一个ApplicationStartingEvent事件

    •  LoggingApplicationListener
    •  BackgroundPreinitializer
    • DelegatingApplicationListener
    • LiquibaseServiceLocatorApplicationListener

2.2. 准备环境变量 Environment

  2.2.1.org.springframework.boot.SpringApplication#prepareEnvironment

       org.springframework.boot.SpringApplication#getOrCreateEnvironment         

new StandardServletEnvironment() ->new StandardEnvironment() ->new AbstractEnvironment()

public AbstractEnvironment() {
this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
this.customizePropertySources(this.propertySources);
} #StandardServletEnvironment
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource("servletConfigInitParams"));
propertySources.addLast(new StubPropertySource("servletContextInitParams"));
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource("jndiProperties"));
} super.customizePropertySources(propertySources);
} #StandardEnvironment
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new MapPropertySource("systemProperties", this.getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", this.getSystemEnvironment()));
}

此时,environment中的propertySources包含servletConfigInitParams,servletContextInitParams,jndiProperties(存在的话,spring.properties中配置spring.jndi.ignore=true,且...待定),systemProperties,systemEnvironment

      org.springframework.boot.SpringApplication#configureEnvironment

 protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
} if (this.addCommandLineProperties && args.length > 0) {
String name = "commandLineArgs";
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
composite.addPropertySource(source);
sources.replace(name, composite);
} else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
} }

      org.springframework.boot.SpringApplicationRunListeners#environmentPrepared

      发布一个ApplicationEnvironmentPreparedEvent

   

  1. ConfigServerBootstrapApplicationListener

    private PropertySource<?> propertySource = new MapPropertySource("configServerClient", Collections.singletonMap("spring.cloud.config.enabled", "false"));
    
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}").equalsIgnoreCase("true") && !environment.getPropertySources().contains(this.propertySource.getName())) {
    environment.getPropertySources().addLast(this.propertySource);
    } }
  2. BootstrapApplicationListener
  3. LoggingSystemShutdownListener
  4. ConfigFileApplicationListener
  5. AnsiOutputApplicationListener
  6. LoggingApplicationListener
  7. BackgroundPreinitializer
  8. ClasspathLoggingApplicationListener
  9. DelegatingApplicationListener
  10. FileEncodingApplicationListener

  

2.3. 初始化ApplicatonContext

     org.springframework.boot.SpringApplication#createApplicationContext

     创建一个AnnotationConfigApplicationContext

     org.springframework.boot.SpringApplication#prepareContext

     发布一个ApplicationContextInitializedEvent

     org.springframework.boot.SpringApplication#refreshContext

        发布一个ContextRefreshedContext,初始化所有的Bean

   

1.springboot启动流程的更多相关文章

  1. SpringBoot启动流程解析

    写在前面: 由于该系统是底层系统,以微服务形式对外暴露dubbo服务,所以本流程中SpringBoot不基于jetty或者tomcat等容器启动方式发布服务,而是以执行程序方式启动来发布(参考下图ke ...

  2. SpringBoot启动流程分析(五):SpringBoot自动装配原理实现

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  3. SpringBoot启动流程分析(六):IoC容器依赖注入

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  4. SpringBoot启动流程分析(一):SpringApplication类初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  5. SpringBoot启动流程分析(二):SpringApplication的run方法

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  6. SpringBoot启动流程分析(三):SpringApplication的run方法之prepareContext()方法

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  7. SpringBoot启动流程分析(四):IoC容器的初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  8. SpringBoot启动流程及其原理

    Spring Boot.Spring MVC 和 Spring 有什么区别? 分别描述各自的特征: Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等:但他们的 ...

  9. springboot启动流程(一)构造SpringApplication实例对象

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 启动入口 本文是springboot启动流程的第一篇,涉及的内容是SpringApplicat ...

  10. SpringBoot 启动流程

    SpringBoot 启动流程 加载 resources/META-INF/spring.factories 中配置的 ApplicationContextInitializer 和 Applicat ...

随机推荐

  1. 安卓开发之ArrayAdapter使用

    package com.lidaochen.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundl ...

  2. 7.使用EXPLAIN 来分析SQL和表结构_2

    possible_keys    ------   显示可能应用在这张表的索引,一个或多个 查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被实际查询使用 key   ------   实际使 ...

  3. pymysql_mysql密码重置方法,连接局域网数据库的解决办法

    https://blog.csdn.net/qq_37176126/article/details/72824106   pymysql模块的操作 https://blog.csdn.net/skh2 ...

  4. Linux基础篇之CentOS的网络配置(DHCP,静态)

    1.启动系统,使用用户名.密码登录系统:  2. 配置网卡(DHCP获取IP地址.静态手动配置IP地址): 网卡的默认信息  DHCP模式修改为(下图): 静态IP地址修改为(下图): 无论哪种配置, ...

  5. PEP8规范 Python

    前言 从很多地方搬运+总结,以后根据这个标准再将python的一些奇技淫巧结合起来,写出更pythonic的代码~ PEP8 编码规范 英文原版请点击这里 以下是@bobo的整理,原文请见PEP8 P ...

  6. JSON 语法 数据格式

    我们先来看下w3cschool对json的定义: JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XM ...

  7. 软raid实现

    几种raid类型,我就不在这里赘述了,企业一般都是用raid卡,所以一般使用软raid的非常少,但是也有用的,所以就写一个软raid的实验吧,其实用处不大. 实验环境:centos6.9 需要的硬件: ...

  8. 【CF1218E】Product Tuples

    题目大意:给定一个长度为 \(N\) 的序列,求从序列中选出 \(K\) 个数的集合乘积之和是多少. 题解: 由于是选出 \(K\) 个数字组成的集合,可知对于要计算的 \(K\) 元组来说是没有标号 ...

  9. BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)

    题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...

  10. HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3

    /* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...