spring-boot配置文件的调查研究

关于spring-boot的配置文件的调查研究如下:


  1. 在单纯的spring-boot工程中,只有application.properties/yml配置文件


  2. 在spring-boot工程中加入了spring-cloud-config的相关依赖,那么bootstrap和application配置文件才会同时生效


  3. *.properties 与 *.yml 扩展名之间的配置文件,只不过是书写的格式不同:一个普通的KV方式,一个是json的超集方式
    *.properties 优先级>*.yml (spring-boot 1.5.10version测试)

  4. 如果在spring-boot工程中加入了spring-cloud-config的相关依赖,那么就有四种类型的配置文件,按照优先级顺序:
    remote-config-server(配置中心配置) > cmd(命令行)/OS properties(操作系统属性) > application > bootstrap


  5. 如果在这四类配置中,拥有了相同的key,那么会按照上述优先级进行属性值设置(只会设置一次)


  6. 特例:spring.profiles.active的属性值解析的优先级,会发生变化!!!
    优先级是:cmd(命令行)/OS properties(操作系统属性)> bootstrap > application

    如果在 "cmd(命令行)/OS properties(操作系统属性)> bootstrap > application" 配置了 spring.profiles.active 
                    remote-config-server(配置中心配置)  的 spring.profiles.active 值会生效,但是行为(The following profiles are active)不会生效
    如果没在 "cmd(命令行)/OS properties(操作系统属性)> bootstrap > application"配置了 spring.profiles.active 
                    remote-config-server(配置中心配置)   spring.profiles.active 值和行为才有效



扩展阅读,spring-cloud-config server的一些特殊设置(in org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties):


Overriding the Values of Remote Properties

The property sources that are added to you application by the bootstrap context are often "remote" (e.g. from a Config Server), and by default they cannot be overridden locally, except on the command line. If you want to allow your applications to override the remote properties with their own System properties or config files, the remote property source has to grant it permission by setting spring.cloud.config.allowOverride=true (it doesn’t work to set this locally). Once that flag is set there are some finer grained settings to control the location of the remote properties in relation to System properties and the application’s local configuration: spring.cloud.config.overrideNone=true to override with any local property source, and spring.cloud.config.overrideSystemProperties=false if only System properties and env vars should override the remote settings, but not the local config files.



你可能感兴趣的:(spring)