linux下修改yml脚本文件编码,Spring Boot 装载自定义yml文件

本文介绍如何在Spring Boot中加载自定义的yml配置文件,由于1.5版本后@configurationProperties的location参数被移除,可以通过实现ApplicationListener接口并在ApplicationEnvironmentPreparedEvent事件中使用YamlPropertySourceLoader加载yml文件。详细代码和事件说明帮助理解加载过程。
摘要由CSDN通过智能技术生成

yml格式的配置文件感觉很人性化,所以想把项目中的.properties都替换成.yml文件,蛋疼的是springboot自1.5以后就把@configurationProperties中的location参数去掉,各种查询之后发现可以用YamlPropertySourceLoader去装载yml文件,上代码

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

ResourceLoader loader = new DefaultResourceLoader();

YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();

List yamlFilePaths = new ArrayList<>();

while(true){

String yamlFilePath = environment.getProperty("load.yaml["+i+"]");

if(yamlFilePath==null){

break;

}

i++;

if("".equals(yamlFilePath)){

continue;

}

yamlFilePaths.add(yamlFilePath);

}

yamlFilePaths.forEach(filePath->{

try {

environment.getProper

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值