SpringBoot 项目启动时读取配置文件内容到Map

实现目标:项目启动时读取配置文件存到Map对象中,在接口调用时直接从Map中获取需要的值
配置文件格式:

route.paths=0x16,0x21

route.0x16.id=order
route.0x16.path=execOrder
route.0x21.id=express
route.0x21.path=execExpress

最终存储的Map格式:

@Data
public class ParameterPath {
    private String id;
    private String path;
}
Map   <String, ParameterPath> map=null;
map中的key为 0x16时value为ParameterPath对象,分别存储id和path数值

实现方法:
通过实现接口EnvironmentAware,重写方法setEnvironment时执行从配置文件获取数值。
从配置文件获取数值利用了Environment 类中的getProperty()方法。

@Configuration
public class InetisIdPathConf implements EnvironmentAware {
       //存储最后的map输出值
    public static Map<String, ParameterPath> inetisRoute = new HashMap<String, ParameterPath>();

    @Override
    public void setEnvironment(Environment environment) {
        initInetisIdAndPath(environment);
    }

    private void initInetisIdAndPath(Environment env) {
        String inetisUrls = env.getProperty("route.paths");
        for (String inetisUrl : inetisUrls.split(",")) {
            ParameterPath parameterPath = new ParameterPath();
            parameterPath.setId(env.getProperty("route." + inetisUrl + ".id"));
            parameterPath.setPath("/" + env.getProperty("route." + inetisUrl + ".path"));
            inetisRoute.put(SybaseToJavaUtil.lTrimAndrTrim(inetisUrl), parameterPath);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值