不同位置配置文件的优先级
相同位置配置文件执行顺序
bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 先于 application.yml 加载
Because of the ordering rules of property sources, the “bootstrap” entries take precedence. However, note that these do not contain any data from bootstrap.yml,which has very low precedence but can be used to set defaults
因为资源文件有次序的规则,所以bootstrap实体具体更高的优先级,然而,注意不是 bootstrap.yml里所有的数据数据都这样,有些的优先级很低,但是被设置成了默认值
应用上下文层次结构
The bootstrap context is the parent of the most senior ancestor that you create yourself. If the child has a property source with the same name as the parent, the value from the parent is not included in the child
bootstrap context是你创建所有context的祖先,如果孩子和父辈有相同配置资源,则父辈的配置资源会生效,而不是孩子
覆盖远程属性
property sources
被bootstrap context
添加到应用通常通过远程的方式,比如”Config Server”。默认情况下,本地的配置文件不能覆盖远程配置,但是可以通过启动命令行参数来覆盖远程配置。如果需要本地文件覆盖远程文件,需要在远程配置文件里设置授权
spring.cloud.config.allowOverride=true
(这个配置不能在本地被设置)。一旦设置了这个权限,你可以配置更加细粒度的配置来配置覆盖的方式,
比如:
- spring.cloud.config.overrideNone=true
覆盖任何本地属性
- spring.cloud.config.overrideSystemProperties=false
仅仅系统属性和环境变量
源文件见PropertySourceBootstrapProperties
自定义启动配置
The bootstrap context can be set to do anything you like by adding entries to /METAINF/spring.factories under a key named org.springframework.cloud.bootstrap.BootstrapConfiguration. This holds a comma-separated list of Spring @Configuration classes that are used to create the context.If you want to control the startup sequence,
classes can be marked with an @Order annotation (the default order is last).
bootstrap context 里还能通过 /METAINF/spring.factories下一个key叫org.springframework.cloud.bootstrap.BootstrapConfiguration添加你自己想要的类实体,这是一个使用逗号分隔的Spring @Configuration类列表,这些类用于创建、上下文。如果你想控制类的启动顺序可以通过@Order注解(默认是在最后)
# spring-cloud-context-1.1.1.RELEASE.jar # spring.factories # AutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\ org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.cloud.bootstrap.BootstrapApplicationListener,\ org.springframework.cloud.context.restart.RestartListener # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\ org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
具体例子见 Spring Cloud使用BootstrapConfiguration配置启动加载项_不夜城的油条的博客-CSDN博客
自定义引导配置来源:Bootstrap Property Sources
The default property source for external configuration added by the bootstrap process is the Spring Cloud Config Server, but you can add additional sources by adding beans of type PropertySourceLocator to the bootstrap context (through spring.factories). As an example, consider the following custom locator:
添加外部配置文件为默认属性是被Spring Cloud Config Server的bootstrap context 处理的,但是你可以通过PropertySourceLocator 向bootstrap context添加额外的资源。例如,下面的自定义定位器
@Configuration public class CustomPropertySourceLocator implements PropertySourceLocator @Override public PropertySource<?> locate(Environment environment) { return new MapPropertySource("customProperty", Collections.<String,Object>singletonMap("property.from.sample.custom.source", "worked as intended")); } }
具体例子见 springboot自定义配置源 - atheva - 博客园
参考文章
SpringCloud入门之常用的配置文件 application.yml和 bootstrap.yml区别 - JackYang - 博客园
SpringBoot配置文件的优先级_Little_fxc的博客-CSDN博客_springboot配置文件的优先级