• Springboot动态替换Properties配置变量


    SpringBoot动态替换Properties配置变量

    package tk.mybatis.springboot.conf;
    
    import cn.hutool.core.io.FileUtil;
    import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
    import org.springframework.boot.origin.OriginTrackedValue;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.core.env.MutablePropertySources;
    import org.springframework.core.env.PropertySource;
    
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class LocalEnvironmentPrepareEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
    
        @Override
        public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
            MutablePropertySources propertySources = event.getEnvironment().getPropertySources();
            Map<String,String> pp=new HashMap<>();
            List<String> strings1 = FileUtil.readLines("d:/conf.properties", StandardCharsets.UTF_8);
            for (String s : strings1) {
                String[] split = s.split("=");
                String put = pp.put(split[0], split[1]);
            }
    
            for (PropertySource<?> propertySource : propertySources) {
                boolean applicationConfig = propertySource.getName().contains("application.properties");
                if (!applicationConfig) {
                    continue;
                }
    // 注意这里 springboot 2.3修改了不能是application.properties了,要放在不是unmodifiedMap的数据里面 Map
    <String, OriginTrackedValue> source = (Map<String, OriginTrackedValue>) propertySource.getSource(); source.put("test.file", OriginTrackedValue.of("/usr/d")); } } }

    application

        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(Application.class);
            app.addListeners(new LocalEnvironmentPrepareEventListener());
            app.run(args);
    
        }
  • 相关阅读:
    Java API操作 上传文件
    在火狐中button标签与a标签冲突事件
    谷歌地图集成步骤
    遇到Error:Execution failed for task ':app:transformClassesWithDexForDebug'的解决方案
    05-IntentFilter的匹配规则
    如何将自己在github写的android library开源,让大家依赖使用
    AS中将module转成library的步骤
    https如何进行加密传输
    对货币数据进行转换——新浪面试
    Android下的缓存策略
  • 原文地址:https://www.cnblogs.com/liran123/p/16200235.html
Copyright © 2020-2023  润新知