Springboot动态修改配置文件

 1.代码

package com.kenick.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CustomApplicationProperties implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        MutablePropertySources propertySources = environment.getPropertySources();

        // 所有属性文件名
        String env = "";
        Iterator<PropertySource<?>> iterator = propertySources.iterator();
        System.out.println(" ================== 所有配置文件名 ==================");
        while(iterator.hasNext()){
            PropertySource<?> next = iterator.next();
            String configFileName = next.getName();
            System.out.println(configFileName);
            if(configFileName.contains("application-")){ // spring只会加载当前环境配置
                env = configFileName.split("application-")[1].replace(".properties]", "");
            }
        }

        // 获取主配置文件
        String mainName = "applicationConfig: [classpath:/application.properties]";
        MapPropertySource propertySource = (MapPropertySource) propertySources.get(mainName);
        Map<String, Object> source = propertySource.getSource();
        System.out.println(" ================== 主配置 ==================");
        source.forEach((k,v) -> {
            System.out.println(k+":"+v.toString());
        });

        // 获取激活配置文件
        System.out.println("当前环境:" + env);
        String activeName = "applicationConfig: [classpath:/application-" + env + ".properties]";
        MapPropertySource activePropertySource = (MapPropertySource) propertySources.get(activeName);
        Map<String, Object> activeSource = activePropertySource.getSource();
        System.out.println(" ================== 当前配置 ==================");
        Map<String, Object> newConfigMap = new HashMap<>();
        activeSource.forEach((k,v) -> {
            System.out.println(k+":"+v.toString());
            newConfigMap.put(k, v.toString()); // value必须要放入String格式
        });
        // 可动态修改配置
        // newConfigMap.replace("log.custom.test", "E:\\tmp\\logs");
        propertySources.replace(activeName, new MapPropertySource(activeName , newConfigMap));
    }

}

 2.配置

org.springframework.boot.env.EnvironmentPostProcessor=com.kenick.config.CustomApplicationProperties

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值