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:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  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. 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).

 

由於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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM