ConfigDataEnvironmentPostProcessor 源码解析
2022-12-01 19:39:07 1 举报
AI智能生成
spring boot ConfigDataEnvironmentPostProcessor 源码解析
作者其他创作
大纲/内容
测试类
spring boot 源码
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessorTests#applyToAppliesPostProcessing
原理
ConfigDataEnvironmentPostProcessor#postProcessEnvironment
#getConfigDataEnvironment
创建一个 ConfigDataEnvironment 对象
初始化 resolvers
<font color="#00ff00">createConfigDataLocationResolvers</font>
接口 ConfigDataLocationResolver
实现类 StandardConfigDataLocationResolver
初始化 loaders
<font color="#00ff00">ConfigDataLoaders</font>
接口 ConfigDataLoader
实现类 StandardConfigDataLoader
实现类 ConfigTreeConfigDataLoader
初始化 contributors
<font color="#00ff00">createContributors(binder)</font>
<font color="#ffff00">遍历 environment.getPropertySources()</font>
systemProperties
environmentProperties
<font color="#ffff00">#getInitialImportContributors</font>
spring.config.import
spring.config.additional-location
spring.config.location
#processAndApply
#processInitial
contributors.withProcessedImports(importer, null)
#processWithoutProfiles
contributors.withProcessedImports(importer, activationContext)
#withProfiles
<font color="#ffff00">添加 additionalProfiles 和 includeProfiles</font>
#processWithProfiles
contributors.withProcessedImports(importer, activationContext)
#applyToEnvironment
#applyContributor
添加 contributor 中的 properySources 到 environment 中
environment.setDefaultProfiles
设置默认的 profile
environment.setActiveProfiles
设置激活的 profile
<font color="#ffff00">withProcessedImports</font>
获取 nextContributor,这里会根据 profile 来判断
如果是 UNBOUND_IMPORT, 那就进行 withBoundProperties
contributor.getImports()
获取 configDataLocation
importer.resolveAndLoad
解析 configDataLocation 并加载为 configData, <font color="#00ff00">解析出来的 Kind 为 UNBOUND_IMPORT</font>
withReplacement
替换之前的 contributor,<font color="#00ff00">这里会标记 importPhase, 所以下次这个contributor就不会获取到了</font>
注意点
一定要理解 Kind 的顺序
INITIAL_IMPORT
UNBOUND_IMPORT
BOUND_IMPORT
<font color="#ffff00">children 是 null</font>
<font color="#ffff00">configDataLocation 不为空,会再次获取这个 contributor</font>
0 条评论
下一页