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. C++ STL 之 stack

    stack 是一种先进后出(first in last out,FILO)的数据结构,它只有一个出口,stack 只允许在栈顶新增元素,移除元素,获得顶端元素,但是除了顶端之外,其他地方不允许存取 元 ...

  2. 14.MySQL主从复制

    1.复制的基本原理 三步骤 + 原理图 1.1 master将改变记录到二进制文件(binary log),这些记录过程叫做二进制日志事件, binary log events 1.2 slave 将 ...

  3. davinci 删除路线站点关系

    删除路线站点关系 DELETE FROM tb_station_info_draw WHERE id in (SELECT stationId FROM tb_road_station_relatio ...

  4. Django获取用户form表单

    首先创建一个Django 的工程项目 前面我们说过了,那到一个项目首先把模板路径,和静态路径在settings.py设置好以后,在开始写代码,写代码也要按照我们以前说的那个工程目录结构写. 现在我们做 ...

  5. Django项目上线的准备工作

    settings文件配置 添加上STATIC_ROOT = os.path.join(BASE_DIR, "/static/") 我的配置项目文件的最终 STATIC_URL = ...

  6. Python3+Appium学习笔记02-环境配置(下)

    配置所需软件及我当前使用的版本: 1)java jdk  1.8.0 2)android sdk  24.4.1 3)Python3  3.7.3 4)Appium-Python-Client 5)n ...

  7. Switch按钮

    使用CSS+HTML5修改原生checkbox为Switch Button .switch { width: 45px; height: 15px; position: relative; borde ...

  8. CSS基础学习-15-1.CSS 浏览器内核

  9. redis四种部署方式

    1.单点 2.主从 3.哨兵 4.集群

  10. PHP常见设计模式

    简单工厂 <?php # 简单工厂 abstract class mycoach { public function __construct(){} } class cultivatecpc e ...