springboot项目中PropertySource如何读取yaml配置文件

 更新时间:2024年01月13日 14:55:43   作者:luffy5459  
这篇文章主要介绍了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)

注意两种方式的区别,以及配置文件需要放置的位置。 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot轻松实现ip解析(含源码)

    SpringBoot轻松实现ip解析(含源码)

    IP地址一般以数字形式表示,如192.168.0.1,IP解析是将这个数字IP转换为包含地区、城市、运营商等信息的字符串形式,如“广东省深圳市 电信”,这样更方便人理解和使用,本文给大家介绍了SpringBoot如何轻松实现ip解析,需要的朋友可以参考下
    2023-10-10
  • 举例讲解Java中synchronized关键字的用法

    举例讲解Java中synchronized关键字的用法

    这篇文章主要介绍了Java中synchronized关键字的用法,针对synchronized修饰方法的使用作出了简单讲解和演示,需要的朋友可以参考下
    2016-04-04
  • Spring中@Value使用详解及SPEL表达式

    Spring中@Value使用详解及SPEL表达式

    这篇文章主要介绍了Spring中@Value使用详解及SPEL表达式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Springboot中的@ConditionalOnBean注解详细解读

    Springboot中的@ConditionalOnBean注解详细解读

    这篇文章主要介绍了Springboot中的@ConditionalOnBean注解详细解读,@ConditionalOnMissingBean注解两个类,一个Computer类,一个配置类,想要完成;如果容器中没有Computer类,就注入备用电脑Computer类,如果有Computer就不注入,需要的朋友可以参考下
    2023-11-11
  • Java实现公用实体类转Tree结构

    Java实现公用实体类转Tree结构

    这篇文章主要为大家介绍了一个Java工具类,可以实现Java公用实体类转Tree结构,文中的示例代码简洁易懂,感兴趣的小伙伴可以参考一下
    2024-10-10
  • java 四舍五入使java保留2位小数示例讲解

    java 四舍五入使java保留2位小数示例讲解

    这篇文章主要介绍了java四舍五入使java保留2位小数示例,大家参考使用
    2013-12-12
  • Java设计模式之代理模式详细解读

    Java设计模式之代理模式详细解读

    这篇文章主要介绍了Java设计模式的代理模式,文中有非常详细的代码示例,对正在学习Java设计模式的小伙伴有很大的帮助,感兴趣的小伙伴可以参考一下
    2021-08-08
  • SpringBoot快速整合RabbitMq小案例(使用步骤)

    SpringBoot快速整合RabbitMq小案例(使用步骤)

    这篇文章主要介绍了SpringBoot快速整合RabbitMq小案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • Java中static的特点

    Java中static的特点

    本文主要介绍了Java中static的特点。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Java处理异常2种机制关键字区别解析

    Java处理异常2种机制关键字区别解析

    这篇文章主要介绍了java处理异常2种机制关键字区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01

最新评论