zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    【Thymeleaf】遇到Current request is not a multipart request不要慌,检查页面中form的属性enctype="multipart/formdata"是否正确就对了
    [CSS]让ul中的li在所属div内成一行居中显示。
    【JS】将yyyyMMdd hh:mm:ss的字符串时间转换为JS时间
    分页资料收集
    【oralce/springboot/jquery】自行实现分页
    【JS】一小时之内显示红饼图标,两小时之内选择黄星图标,否则显示时间
    【JavaScript】给动态生成的链接动态的绑定事件
    VBA在Excel中的应用(四)
    在MOSS 2007中自定义DataView Webpart的数据显示样式
    ASP.NET中的缩略图应用
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/9298419.html
Copyright © 2011-2022 走看看