springboot项目中PropertySource如何读取yaml配置文件
PropertySource如何读取yaml配置文件
springboot项目中,当我们使用@Value注解读取配置属性,默认的配置文件是properties类型文件,如果一些配置来自yaml格式配置文件,那么就需要做一个配置。
PropertySource注解提供了factory属性,可以设置yaml格式文件加载工厂类。
下面介绍如何自定义factory实现yaml配置文件加载。
项目准备
maven工程pom.xml增加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>自定义YamlPropertySourceFactory
package com.xxx.web.config;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
List<PropertySource<?>> list = new YamlPropertySourceLoader().load(resource.getResource().getFilename(),resource.getResource());
if(list != null && list.size() > 0)
return list.get(0);
return null;
}
}yml配置文件使用
package com.xxx.web.controller;
import com.xxx.web.config.YamlPropertySourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/test/v1")
@PropertySource(value = {"file:conf/test.yml"},factory = YamlPropertySourceFactory.class)
public class TestController {
@Value("${business.errorCode}")
private int errorCode;
@Value("${business.msg}")
private String msg;
@Value("${business.data}")
private String data;
@RequestMapping(value = "/status",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity queryStatus(){
Map<String,Object> result = new HashMap(){
{
put("msg",msg);
put("errorCode",errorCode);
put("data",data);
}
};
return ResponseEntity.ok(result);
}
}这里为了配合项目配置,配置文件放在工程根目录下的conf/test.yml
business: errorCode: 2 msg: success, data: hallo
启动项目,我们可以访问地址http://localhost:8080/test/v1/status,可以看到,返回结果为配置文件中设置的值。

这里值得注意的是,@PropertySource在指定配置文件位置的时候,使用的是file,这种方式指定的路径,是相对于项目根路径来的,所以我们的配置文件没有放在resources目录下,
如果放在resources目录下,要通过file访问到,那么,位置需要设置成这样:
@PropertySource(value={"file:src/main/resources/conf/test.yml"},factory=YamlPropertySourceFactory.class)这里还可以通过classpath的方式来指定,如果conf/test.yml放在了resources类路径下。
就可以这样设置:
@PropertySource(value = {"classpath:conf/test.yml"},factory = YamlPropertySourceFactory.class)注意两种方式的区别,以及配置文件需要放置的位置。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
利用Java Apache POI 生成Word文档示例代码
本篇文章主要介绍了利用Java Apache POI 生成Word文档示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05
springboot代码,注解配置获取yml,properties文件的map即键值对
这篇文章主要介绍了springboot代码,注解配置获取yml,properties文件的map即键值对,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-02-02
SpringBoot项目速度提升之延迟初始化(Lazy Initialization)详解
延迟初始化(Lazy Initialization)是一种在需要时才创建或加载对象的策略,以减少启动时间和资源消耗,本文就来讲讲延迟初始化的具体使用吧2023-05-05
knife4j+springboot3.4异常无法正确展示文档
本文主要介绍了knife4j+springboot3.4异常无法正确展示文档,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2025-01-01
Java @Transactional与synchronized使用的问题
这篇文章主要介绍了Java @Transactional与synchronized使用的问题,了解内部原理是为了帮助我们做扩展,同时也是验证了一个人的学习能力,如果你想让自己的职业道路更上一层楼,这些底层的东西你是必须要会的2023-01-01


最新评论