spring-boot-EnvironmentPostProcessor
原理:
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的更多相关文章
- Spring Boot 启动(四) EnvironmentPostProcessor
Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...
- Spring Boot启动流程详解(一)
环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- Spring Boot Application
spring boot默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,spring boot允许你自定义一个application.pro ...
- Spring Boot 启动(二) 配置详解
Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...
- Spring Boot 启动(二) Environment 加载
Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...
- Spring Boot实现热部署
在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目. 引用devtools依赖 <dependency> <groupId>org ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- spring boot 2.0 源码分析(四)
在上一章的源码分析里,我们知道了spring boot 2.0中的环境是如何区分普通环境和web环境的,以及如何准备运行时环境和应用上下文的,今天我们继续分析一下run函数接下来又做了那些事情.先把r ...
- spring boot 自定义属性覆盖application文件属性
参考 Spring boot源码分析-ApplicationListener应用环境: https://blog.csdn.net/jamet/article/details/78042486 加载a ...
随机推荐
- yaml语法及格式校验
基本语法 1.yml文件以缩进代表层级关系 2.缩进不允许使用tab只能使用空格 3.空格的个数不重要,只要相同层级的元素左对齐即可 4.大小写敏感 5.数据格式为,名称:(空格)值 也就是说,如果冒 ...
- 免费报表软件下载推荐------值得办公小白下载的Web报表工具
Smartbi免费报表软件更是国内报表产品的新高峰,它直接使用Excel作为报表设计器,易用性.功能性.运行速度都得到了大幅提升,遥遥领先竞品.该产品以"真Excel"为最大特色, ...
- Oracle建表和SQL语句的增删改
创建表 --建表语法结构: /* CREATE TABLE 表名( 字段名1 数据类型, 字段名2 数据类型 .... ); */ --建一张老师表,里面包含老师编号,老师姓名,老师联系方式,老师入职 ...
- HTML背景图加载过慢解决思路
压缩图片的大小 第一个压缩图片网站 第二个压缩图片网站
- 2020CCPC长春F. Strange Memory
题目大意 一棵以 \(1\) 为根的 \(n(2\leq n\leq 10^5)\) 的树,每个节点 \(i\) 有权值 \(a_{i}(1\leq a_{i}\leq 10^6)\) ,求 \(\s ...
- Chrome:开发者模式下复制Element下的代码
Element模块下的代码只能一行一行复制,想要复制一个代码块,可以把该代码块先收起来,再对这个收起来的代码块进行复制就OK了
- Mysql的用户管理与授权
Mysql用户管理 本人使用的是Mysql8.0的版本,可能会有一些语句不兼容: 1.用户管理 在Mysql中支持创建账户,并给账户分配权限:例如只拥有数据库A操作的权限.只拥有数据库B中某些表的权限 ...
- 震撼的Linux全景图:业界成熟的内核架构长什么样?
1)Linux怎么来的? Linus 为了方便访问大学服务器中的资源 ,在自己的机器上写了一个文件系统和硬盘驱动,这样就可以把自己需要的资源下载到自己的机器中.随后linus把这款操作系统雏形开源,成 ...
- think php 登录 (session+验证码)
..........表单页面 <!DOCTYPE html> {__NOLAYOUT__} <html lang="en"> <head> &l ...
- ElasticSearch安装 --- windows版
文件下载地址: 链接:https://pan.baidu.com/s/1_Cy_Sy1-vOvsUxliM-EZHQ 提取码:qpcv 定义: Elasticsearch 是一个分布式的搜索和分析引擎 ...