Springboot属性加载与覆盖优先级与SpringCloud Config Service配置
参考官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Spring Boot uses a very particular PropertySource
order that is designed to allow sensible overriding of values. Properties are considered in the following order:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). @TestPropertySource
annotations on your tests.properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property). ServletConfig
init parameters.ServletContext
init parameters.- JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that has properties only inrandom.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants). - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). @PropertySource
annotations on your@Configuration
classes.- Default properties (specified by setting
SpringApplication.setDefaultProperties
).
由于SpringCloud config的配置文件属于上面的第14/12,理论上优先级相对java -jar命令行参数来说是要低的,但cloud的配置确默认是:远程覆盖本地(OS/java -jar参数等)overrideSystemProperties = true,需要特别注意。
package org.springframework.cloud.bootstrap.config; import org.springframework.boot.context.properties.ConfigurationProperties; /** * Properties for Spring Cloud Config bootstrap. * * @author Dave Syer */ @ConfigurationProperties("spring.cloud.config") public class PropertySourceBootstrapProperties { /** * Flag to indicate that the external properties should override system properties. * Default true. */ private boolean overrideSystemProperties = true; /** * Flag to indicate that {@link #isOverrideSystemProperties() * systemPropertiesOverride} can be used. Set to false to prevent users from changing * the default accidentally. Default true. */ private boolean allowOverride = true; /** * Flag to indicate that when {@link #setAllowOverride(boolean) allowOverride} is * true, external properties should take lowest priority and should not override any * existing property sources (including local config files). Default false. */ private boolean overrideNone = false; public boolean isOverrideNone() { return this.overrideNone; } public void setOverrideNone(boolean overrideNone) { this.overrideNone = overrideNone; } public boolean isOverrideSystemProperties() { return this.overrideSystemProperties; } public void setOverrideSystemProperties(boolean overrideSystemProperties) { this.overrideSystemProperties = overrideSystemProperties; } public boolean isAllowOverride() { return this.allowOverride; } public void setAllowOverride(boolean allowOverride) { this.allowOverride = allowOverride; } }