码迷,mamicode.com
首页 > 编程语言 > 详细

36 SpringBoot 在系统配置文件中动态加载配置

时间:2019-11-03 10:33:48      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:sys   set   stp   system   proc   new   cat   orm   manage   

1. 动态加载配置

package com.thc.rcm.system.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @author shizhanwei
 * 1.代替yml文件中原服务地址的配置,其它服务名称变动需修改这里
 * 2.yml文件中的下面配置可以删除了
 * 例如:
 * feign:
 *   base: /
 *   rcm: rcm/
 *
 */
@Component
public class AppEnvPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        MutablePropertySources propertySources = environment.getPropertySources();
        Properties properties = new Properties();
        properties.setProperty("feign.base","/");
        properties.setProperty("feign.rcm","rcm/");
        properties.setProperty("feign.warehouse","warehouse/");
        properties.setProperty("feign.sob","sob/");
        properties.setProperty("feign.phr","thc-phr/");
        properties.setProperty("feign.passport","c-union/");
        properties.setProperty("feign.market","market/");
        properties.setProperty("feign.mall","c-mall/");
        properties.setProperty("feign.cunion","c-union/");
        properties.setProperty("feign.insurance","insurance/");
        properties.setProperty("feign.permission","thc-platform-core/");
        properties.setProperty("feign.msg","msg/");
        properties.setProperty("feign.epay","epay/");
        properties.setProperty("feign.arrange","arrange/");
        properties.setProperty("feign.medicalrecord","medical-record/");
        properties.setProperty("feign.workbench","workbench/");
        properties.setProperty("feign.pricemanage","pricemanage/");
        properties.setProperty("feign.process-engine","process-engine/");
        propertySources.addLast(new PropertiesPropertySource("thc_apps",properties));
    }
}

 

 

2. 在代码中引用:

    @Autowired
    Environment env;

    @PostMapping(value = "/testEnv")
    public Object testEnv() {
        return env.getProperty("feign.pricemanage");
    }

 

 

 

 

36 SpringBoot 在系统配置文件中动态加载配置

标签:sys   set   stp   system   proc   new   cat   orm   manage   

原文地址:https://www.cnblogs.com/guchunchao/p/11785182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!