springboot 2.x源码笔记- 配置文件加载 ConfigFileApplicationListener

本人博客原地址:springboot 2.x源码笔记- 配置文件加载 ConfigFileApplicationListener
创作时间: 2019.06.12 18:14:06

springboot的源码(spring)主要分为几个部分

1、构造SpringApplication,完成spring.factories文件中Initializers与Listeners的加载
2、加载配置文件,通过ConfigFileApplicationListener
3、加载BeanDefinitionRegistryPostProcessor与BeanFactoryPostProcessor完成bean的定义包装(非生成实例)
4、生成bean实例以及初始化

本文主要针对第二点,主要看如何加载配置文件,即如何加载环境信息,主要通过ConfigFileApplicationListener完成

基于springboot2.1.4

项目地址:https://gitee.com/eshin/springbootdemo.git#autotest

org.springframework.boot.SpringApplication#run(java.lang.String…)–>org.springframework.boot.SpringApplication#prepareEnvironment
该方法主要看两部分

  • 1、系统环境信息配置初始化加载org.springframework.boot.SpringApplication#getOrCreateEnvironment

有图可以看出,在构造StandardServletEnvironment的时候,父类的构造器依次执行,构造后的StandardServletEnvironment就包含了如下的propertySource,主要是系统参数

  • 2、发布environmentPrepared事件,ConfigFileApplicationListener监听到后执行

environmentPrepared事件的发布,此处不做详述,可参考此处,其中监听到事件的Listener就有ConfigFileApplicationListener,其实现了EnvironmentPostProcessor接口和SmartApplicationListener接口。
因此在org.springframework.boot.context.config.ConfigFileApplicationListener#onApplicationEnvironmentPreparedEvent遍历执行EnvironmentPostProcessor的时候,就会执行到org.springframework.boot.context.config.ConfigFileApplicationListener#postProcessEnvironment,然后通过org.springframework.boot.context.config.ConfigFileApplicationListener#addPropertySources执行到org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load()
在上图中,this.propertySourceLoaders在spring.factories中配置,可自定义loader并配置。

  • initializeProfiles();中会根据系统参数设置activeProfiles,如果有设置的话,没有就只有default一个。可以在运行springboot项目的时候设置spring.profiles.active,或者spring.profiles.include。本文的前提是没有在系统参数中设置这两个变量,流程差不多,就是在this.profiles.poll()多走几个循环而已。
  • 进入org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load(Profile profile, DocumentFilterFactory filterFactory,DocumentConsumer consumer)
  • 进入org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#loadForFileExtension->org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load(PropertySourceLoader loader, String location, Profile profile,DocumentFilter filter, DocumentConsumer consumer)
    图中中间位置
    其中List<Document> documents = loadDocuments(loader, name, resource);
    然后回到load方法中

最终通过loaded.forEach((document) -> consumer.accept(profile, document));将加载到的配置放入environment的properSourceList中
其中MutablePropertySources::addLast的方法引用方式比较少见,可参考此处

至此,整个environment的配置加载过程就完成了。

3、配置项的注入

配置项的注入是要等到bean的实例化后初始化阶段,参考这里.实例化后,会通过org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties–>org.springframework.beans.factory.annotation.InjectionMetadata#inject—>org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredMethodElement#inject—>org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency—>org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency
—>org.springframework.beans.factory.support.AbstractBeanFactory#resolveEmbeddedValue然后进入org.springframework.context.support.PropertySourcesPlaceholderConfigurer#processProperties(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.core.env.ConfigurablePropertyResolver)中已经定义的StringValueResolver
PropertySourcesPlaceholderConfigurer实现了BeanFactoryPostProcessor的postProcessBeanFactory接口方法,在bean的定义阶段就已经执行其实现的接口方法,将StringValueResolver添加到beanFactory的embeddedValueResolvers中进入StringValueResolver—>org.springframework.core.env.AbstractPropertyResolver#resolveRequiredPlaceholders–>org.springframework.core.env.AbstractPropertyResolver#doResolvePlaceholders–>org.springframework.util.PropertyPlaceholderHelper#replacePlaceholders(java.lang.String, org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver)–>org.springframework.util.PropertyPlaceholderHelper#parseStringValue[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gVNHDF2e-1612235883386)(https://upload-images.jianshu.io/upload_images/13932735-73ec48883ce29aa8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]—>org.springframework.core.env.PropertySourcesPropertyResolver#getPropertyAsRawString

总结:

  • 1、当系统启动参数中配置指定的目录spring.config.location,和文件名spring.config.name,则直接从读取改配置文件,否则使用默认路径,查找顺序为file:./config/,file:./,classpath:/config/,classpath:/,默认文件名为application。
  • 2、只要加载的配置文件中包含spring.profiles.active,且对应的文件没有加载过,那下次循环中就会继续加载对应的配置文件。
  • 3、每个实现org.springframework.boot.env.PropertySourceLoader的loader,都需要指定对应支持的拓展名。
  • 4、若有多个loader对相同拓展名的配置文件处理,优先级高的loader加载,后续的loader处理时略过。
  • 5、读取配置时从propertySourceList中按顺序查找,先放入list中的source中读到了配置,就不往后面查找,因此配置的生效顺序为:系统参数->file:./config/ -> file:./ -> classpath:/config/ -> classpath:/
    指定了profile的要比没有指定的优先。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值