博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@PropertySource 分环境读取配置
阅读量:7046 次
发布时间:2019-06-28

本文共 2698 字,大约阅读时间需要 8 分钟。

工作的时候,一般来说代码都是分环境的,比如dev,test,prd什么的,在用到@PropertySource 注解的时候,发现好像不能根据环境读取自定义的.properties文件,比如我有个systemProperties-dev.properties文件,一开始只是systemProperties-${spring.profiles.active}.properties这样的方式勉强能用,但是后来当我的环境变量变成多环境的时候,也就是spring.profiles.active = dev,test这样的是,这个方法就不奏效了,(多傻啊,其实早就想到了,他会直接在“-”后面拼了一个“dev,test”)然后在网上看了看资料,参考了以下的一篇文章,然后参照了下源码,用了一个比较简单,但是很难看的方法实现了:P(感觉也是暂时解决问题。)。

参照文章:

主要思想,重写PropertySourceFactory,在PropertySourceFactory中,重新取得resource,

SystemProperties.java


@Component@PropertySource(name="systemConfig", value = {"classpath:/systemConfig-${spring.profiles.active}.properties"}, factory = SystemPropertySourceFactory.class)public class SystemProperties {    // 自己的内容.... }

这里指定了 factory = SystemPropertySourceFactory.class,接下来

SystemPropertySourceFactory.java


@Configurationpublic class SystemPropertySourceFactory implements PropertySourceFactory {    @Override    public PropertySource
createPropertySource(String name, EncodedResource encodedResource) throws IOException { FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader(); //取得当前活动的环境名称(因为直接获取spring.profiles.active 失败,所以才把环境名称拼在文件名后面来拿) //其实感觉应该有可以直接取的方法比如从环境里取 String[] actives = encodedResource.getResource().getFilename().split("\\.")[0].replace(name + "-", "").split(","); //如果只有一个,就直接返回 if (actives.length <= 1) { return (name != null ? new ResourcePropertySource(name, encodedResource) : new ResourcePropertySource(encodedResource)); } //如果是多个 List
inputStreamList = new ArrayList<>(); String suffix = fileproperty[1]; //遍历后把所有环境的url全部抓取到list中 Arrays.stream(actives).forEach(active -> { InputStream in = this.getClass().getResourceAsStream("/" + name.concat("-" + active).concat(".").concat(suffix)); if (in != null) { inputStreamList.add(in); } }); if (resourceUrls != null && resourceUrls.size() > 0) { //串行流,将多个文件流合并车一个流 SequenceInputStream inputStream = new SequenceInputStream(Collections.enumeration(inputStreamList)); //转成resource InputStreamResource resource = new InputStreamResource(inputStream); return (name != null ? new ResourcePropertySource(name, new EncodedResource(resource)) : new ResourcePropertySource(new EncodedResource(resource))); } else { return (name != null ? new ResourcePropertySource(name, encodedResource) : new ResourcePropertySource(encodedResource)); } }}

这样实现后,就能将多个环境的Property文件加载进去了。

然后是关于spring.profiles.active 为什么要这么取,我试过@value,和用Environment 对象,都取不到,可能跟bean创建的先后顺序有关。没有继续调查,希望知道原因的朋友能帮忙解答~

转载地址:http://yckol.baihongyu.com/

你可能感兴趣的文章
CoreCLR文档翻译 - GC的设计
查看>>
js-ES6学习笔记-Proxy(2)
查看>>
Spring Boot下Druid连接池+mybatis
查看>>
Session与Cookie解析
查看>>
Java实现二叉排序树的插入、查找、删除
查看>>
Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist
查看>>
UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)
查看>>
EventBus In eShop -- 解析微软微服务架构Demo(四)
查看>>
使用Python写的第一个网络爬虫程序
查看>>
找到当前mysql group replication 环境的primary结点
查看>>
正确的C++/C堆栈
查看>>
使用Swagger生成Spring Boot REST客户端(支持Feign)(待实践)
查看>>
solr6.6 配置拼音分词
查看>>
datatables参数配置详解
查看>>
Composer 结合 Git 创建 “服务类库”
查看>>
Redis线程模型
查看>>
修改firefox/chrome浏览器的UserAgent
查看>>
sql 基础练习 计算7天各个时间点的总和 group by order mysql一次查询多个表
查看>>
MySQL高级-索引优化
查看>>
SQL中Group By的使用
查看>>