springboot项目中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)

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值