spring boot 自定義屬性覆蓋application文件屬性


參考

Spring boot源碼分析-ApplicationListener應用環境:
https://blog.csdn.net/jamet/article/details/78042486

加載application資源文件源碼分析:
https://blog.csdn.net/liaokailin/article/details/48878447

ConfigFileApplicationListener 主要實現了以下接口

    EnvironmentPostProcessor:用於環境的后處理

    SmartApplicationListener:是ApplicationListener的擴展,進一步暴露事件類型。

    Ordered:用於將對象排序

ConfigFileApplicationListener類的解釋

 通過類上的注釋,我們可以知道關於該類的一些信息,

 1.他默認會從classpath: 、file:./ 、classpath:config/ 、 file:./config/  加載'application.properties' 和/或 'application.yml'

 2.其他配置也會根據active profiles 進行 加載 , 
 如 active 此時被設置成 web, spring 加載的時候也會去加載 application-web.properties 和 application-web.yml   

加載項目配置文件時,對應 propertySources 的名稱如下:

[bootstrap,commandLineArgs,systemProperties,systemEnvironment,
random,servletConfigInitParams,servletContextInitParams,
jndiProperties,applicationConfig: [classpath:/application-console_dev.properties],applicationConfig: 
[classpath:/config/application.properties],applicationConfig: 
[classpath:/application.properties],bootstrapProperties,applicationConfig: 
[classpath:/bootstrap.properties],Management 
Server,applicationConfigurationProperties,
defaultProperties,springCloudClientHostInfo]

如果在 applicationConfig 名稱的前面添加屬性,則項目配置文件中的屬性不會覆蓋

public class ConsoleDomainPostrocessor implements EnvironmentPostProcessor, Ordered {

    private static final String PROPERTY_SOURCE_NAME = "xxProperties";

    public static final DOMAIN = "domain";

    private int order = ConfigFileApplicationListener.DEFAULT_ORDER + 20;

    private static Map<String, String> consoleUrlList = new HashMap<>();


    static {
        consoleUrlList.put("consoletest", "http://ecc.consoletest.jcloudec.com");
    }

    @Override
    public int getOrder() {
        return order;
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        Map<String, Object> map = new HashMap<>();

        EccConsoleProperties target = new EccConsoleProperties();
        RelaxedDataBinder binder = new RelaxedDataBinder(target,
                EccConsoleProperties.ECC_CONSOLE);
        binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));

        //默認開啟
        boolean enabled = target.isEnabled();

        if (enabled) {
            if (environment.getActiveProfiles().length > 0 &&
                    (!Arrays.asList(environment.getActiveProfiles()).contains("default"))) {

                String[] activeProfiles = environment.getActiveProfiles();
                String curentConsoleUrl = consoleUrlList.get(activeProfiles[0]);

                if (StringUtils.isNotBlank(curentConsoleUrl)) {
                    map.put(DOMAIN, curentConsoleUrl);
                } else if (StringUtils.isNotBlank(target.getDomain())) {
                    map.put(DOMAIN, target.getDomain());
                } else {
                    map.put(DOMAIN, "http://xx.com");
                }
                System.out.println(String.format("activeProfiles:[%s],console domain:[%s]", activeProfiles[0], map.get(CC_CONSOLE_DOMAIN)));

                MapPropertySource propertySource = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
                
                // 將屬性添加到 application 文件前,這樣application 就不會覆蓋屬性了
                environment.getPropertySources().addBefore(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME, propertySource);
            }
        }
    }
}

在 META-INF/spring.factories 文件內容添加

org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.ConsoleDomainPostrocessor


免責聲明!

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



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