原理:

1-从启动类入口的run方法进入:
public ConfigurableApplicationContext run(String... args) {
-SpringApplicationRunListeners listeners = getRunListeners(args);
-ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);

监听器:EventPublishingRunListener

2-prepareEnvironment方法:

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();//用于创建一个存储变量信息的environment
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}


3-configureEnvironment(environment, applicationArguments.getSourceArgs());加载基本的配置
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
}
configurePropertySources(environment, args);
configureProfiles(environment, args);
}

加载的配置文件包括下面:

4-prepareEnvironment-中调用listeners.environmentPrepared(environment);
这个监听器就是我们上面的EventPublishingRunListener
继续跟进进出会在EventPublishingRunListner中找到:

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
this.initialMulticaster
.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}//新建事件ApplicationEnvironmentPreparedEvent,

5-寻=寻找监听ApplicationEnvironmentPreparedEvent事件的监听器:ConfigFileApplicationListener
里面有两个个方法

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
} private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
postProcessors.add(this);
AnnotationAwareOrderComparator.sort(postProcessors);
for (EnvironmentPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
}
}
for (EnvironmentPostProcessor postProcessor : postProcessors) {
//对所有实现EnvironmentPostProcessor接口的实现类执行内部方法.postProcessEnvironment
postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
}

6-官方必须从META-INF/spring.factories:6

在List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();中
List<EnvironmentPostProcessor> loadPostProcessors() {
return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader());
} SpringFactoriesLoader的一个静态函数:loadFactories

spring-boot-EnvironmentPostProcessor的更多相关文章

  1. Spring Boot 启动(四) EnvironmentPostProcessor

    Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...

  2. Spring Boot启动流程详解(一)

    环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...

  3. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  4. Spring Boot Application

    spring boot默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,spring boot允许你自定义一个application.pro ...

  5. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  6. Spring Boot 启动(二) Environment 加载

    Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...

  7. Spring Boot实现热部署

    在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目. 引用devtools依赖 <dependency> <groupId>org ...

  8. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  9. spring boot 2.0 源码分析(四)

    在上一章的源码分析里,我们知道了spring boot 2.0中的环境是如何区分普通环境和web环境的,以及如何准备运行时环境和应用上下文的,今天我们继续分析一下run函数接下来又做了那些事情.先把r ...

  10. spring boot 自定义属性覆盖application文件属性

    参考 Spring boot源码分析-ApplicationListener应用环境: https://blog.csdn.net/jamet/article/details/78042486 加载a ...

随机推荐

  1. 『无为则无心』Python面向对象 — 60、魔法属性

    目录 1.魔法属性__name__ 2.魔法属性__bases__ 3.魔法属性__mro__ 4.魔法属性__doc__ 5.魔法属性__module__ 和__class__ 6.魔法属性__di ...

  2. 网络测试仪实操手册 RENIX 机框管理

    本文主要阐述信而泰BIGTAO系列 网络测试仪器机框相关操作方法.文章分为机框添加.机框删除.机框重启.机框关机四部分. 第一部分:机框添加 1.添加过程 1.1打开软件 1.2添加端口 1.3输入I ...

  3. 从数据分析系统总架构理解BI工具的价值所在

    ​现如今,应用商业智能BI工具的企业是越来越多了,由此也可见企业对数据分析的重视.因此,掌握一定的数据分析知识对"打工人"来说是非常重要的.现在小编就来跟大家一起来了解一下商业智能 ...

  4. C# 2进制、8进制、10进制、16进制...各种进制转换

    在.NET Framework中,System.Convert类中提供了较为全面的各种类型.数值之间的转换功能. 其中的两个方法可以轻松的实现各种进制的数值间的转换: Convert.ToInt32( ...

  5. 洛谷P4322.最佳团体

    题目大意 一个 \(n(1\leq n\leq 2500)\) 个节点的森林,每个点 \(i\) 有权值 \(s_{i},p_{i}(0<s_{i},p_{i}\leq 10^4)\) 以及父亲 ...

  6. Spark on Yarn出现hadoop.compression.lzo.LzoCodec not found问题发现及解决

    问题描述: spark.SparkContext: Created broadcast 0 from textFile at WordCount.scala:37 Exception in threa ...

  7. Pycharm:运行程序时,不额外打开一个Console

    每次运行程序,比如A.py,都会额外生成一个Console,排列成一排的 A(2),A(3),... 那么如何关闭呢? 答案是:在Settings->Console中,勾选  'Use exis ...

  8. net core or Linux

    某用户执行net core sdk 版本不生效 sudo chmod +x /home/username/netcore3.1sdk/dotnet //某个用户执行新版本net core sdk

  9. consul-常用命令

    1.consul 是B/C架构.服务端和客户端包是一样的.差别在于启动时候的参数. --客户端 ./consul agent -join=172.29.2.65:8301 -bind=172.29.3 ...

  10. c/c++ 内存泄漏分析

    Valgrind: https://zhuanlan.zhihu.com/p/111556601 valgrind输出结果分析 valgrind输出结果会报告5种内存泄露,"definite ...