springboot 配置资源统一管理

在一个springboot的项目中创建一个加载资源的类
LoadPropertiesConfig


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

public class LoadPropertiesConfig implements EnvironmentPostProcessor, Ordered {
    private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(LoadPropertiesConfig.class);

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        //此处可以http方式 到配置服务器拉取一堆公共配置+本项目个性配置的json串,拼到Properties里
        Properties properties = new Properties();
        try {
            properties.load(
                    new FileInputStream(new File("D:\\service\\demo-parents\\springboot-app\\application.properties")));
        } catch (FileNotFoundException e) {
            LOG.error("error message", e);
        } catch (IOException e) {
            LOG.error("error message", e);
        }
        MutablePropertySources propertySources = environment.getPropertySources();
        //addLast 结合下面的 getOrder() 保证顺序 读者也可以试试其他姿势的加载顺序
        propertySources.addLast(new PropertiesPropertySource("thirdEnv", properties));
    }

    @Override
    public int getOrder() {
        //  +1 保证application.propertie里的内容能覆盖掉本配置文件中默认的
        // 如果不想被覆盖 可以去掉 +1  或者 -1  试试
        return Integer.MIN_VALUE;
    }
}

这样就实现了用远程资源覆盖本地资源的情况
测试代码


@SpringBootApplication
@ComponentScan
public class Application {



    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类PropertiesCheckController.java的实现描述:验证自定义配置文件
 * 
 * @author xupeng 2017年3月16日 下午4:04:24
 */
@RestController
public class PropertiesCheckController {

    @Value("${name}")
    private String name;

    @RequestMapping("/showName")
    public String showName() {
        return name;
    }
}

本地配置文件

name=ceshi

远程配置文件

name=name

用以上代码测试,当没有加载到远程资源的时候访问showName
返回的是ceshi当资源被覆盖之后就会显示name

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值