SpringBoot 内置的 EnvironmentPostProcessor,在接收到 ApplicationEnvironmentPreparedEvent 事件中被触发调用
SpringCloud 启动时,JsonProcessor 可获取到的 PropertySource 为
0 = {MapPropertySource@2105} ****"MapPropertySource {name='bootstrap'}"
1 = {MapPropertySource@2106} "MapPropertySource {name='systemProperties'}"
2 = {SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource@2107} "OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}"
SpringBoot 启动时,JsonProcessor 可获取到的 PropertySource 为
0 = {MapPropertySource@2106} "MapPropertySource {name='systemProperties'}"
1 = {SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource@4050} "OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}"
2 = {BootstrapApplicationListener$ExtendedDefaultPropertySource@4051} "ExtendedDefaultPropertySource {name='defaultProperties'}"
sources = {CompositePropertySource@4061} "CompositePropertySource {name='defaultProperties', propertySources=[OriginTrackedMapPropertySource {name='applicationConfig: [classpath:/bootstrap-dev.yml]'}, OriginTrackedMapPropertySource {name='applicationConfig: [classpath:/bootstrap.yml]'}]}"
、
1-3 由 Devtool 或者测试实现类完成(在调用 SpringApplication#run 前置入)
4 由 SpringApplication#prepareEnvironment#configurePropertySources
5 由 SpringApplicationJsonEnvironmentPostProcessor 处理,优先级高于 command line
6-10,17 在创建时设置,其中 6,7 Servlet 相关在 Servlet 环境加载前是 Stub 状态(非可用),见 SpringApplication#prepareEnvironment#configureEnvironment
ConfigFileApplicationListener#postProcessEnvironment 处理 配置优先级 11-15
16 在解析 Bean 定义期间处理,如果发现 beanDefination 是注解类型,会调用 ConfigurationClassParser 进行处理,实际委托给了 DefaultPropertySourceFactory#createPropertySource 处理
spring boot 优先级
1. Devtools global settings properties on your home directory (~/.spring-boot-
devtools.properties when devtools is active).
2. @TestPropertySource annotations on your tests.
3. @SpringBootTest#properties annotation attribute on your tests.
4. Command line arguments.
5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable
or system property).
6. ServletConfig init parameters.
7. ServletContext init parameters.
8. JNDI attributes from java:comp/env.
9. Java System properties (System.getProperties()).
10. OS environment variables.
11. A RandomValuePropertySource that has properties only in random.*.
12. Profile-specific application properties outside of your packaged jar (application-
{profile}.properties and YAML variants).
13. Profile-specific application properties packaged inside your jar (application-
{profile}.properties and YAML variants).
14. Application properties outside of your packaged jar (application.properties and YAML
variants).
15. Application properties packaged inside your jar (application.properties and YAML variants).
16. @PropertySource annotations on your @Configuration classes.
17. Default properties (specified by setting SpringApplication.setDefaultProperties).
spring cloud 控制代码逻辑