专业的JAVA编程教程与资源

网站首页 > java教程 正文

Spring Boot中通过@PropertySource注解读取yaml或yml配置文件

temp10 2025-05-08 05:41:25 java教程 4 ℃ 0 评论

一、@PropertySource注解

@PropertySource加载指定的配置文件。

将一切配置全部写在全局配置文件中,是不可想象的。项目中不可避免存在多个配置文件。

Spring Boot中通过@PropertySource注解读取yaml或yml配置文件

@PropertySource 可以根据需要加载指定的配置文件,将配置文件中的属性注入到系统环境中。@ConfigurationProperties 默认从全局配置文件获取配置。

这里将person的属性配置单独写在person.yml文件中,并从全局配置文件中注释掉person的属性配置。

因为yaml语法很简洁,写yaml配置文件也比较方便。

通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties配置文件是没问题的。但是,换成yaml文件,发现都读取不到属性值。

这是因为@PropertySource默认不支持yaml读取,我们改成@Value注解也是可以读取的,不过属性一堆的话,一个一个读取也是很繁琐的。不过,可以通过自定义开发来实现对yaml配置文件的支持。



二、为什么@PropertySource 注解默认不支持yaml配置文件?

下面简单分析一下源码。

1. @PropertySource 源码

根据上面的注释,默认使用
DefaultPropertySourceFactory类作为资源文件加载类。

进一步调用Spring框架底层的PropertiesLoaderUtils工具类进行读取资源文件。

然后调用
DefaultPropertiesPersister的load方法:


最终加载配置文件的方法,就是下面的load0()方法。

从源码可以看出,这个方法是一行一行地读取,然后根据冒号、等于号、空格等进行校验,经过一系列遍历之后获取key和value,而yaml语法是以缩进来辨别的,这个方法不支持yaml文件的读取。


三、自定义yaml配置文件读取的工厂类

模仿
DefaultPropertySourceFactory写一个yaml配置文件读取的工厂类。

完整代码如下所示:

package com.rickie.springboot.core;


import org.springframework.boot.env.YamlPropertySourceLoader;

import org.springframework.core.env.PropertiesPropertySource;

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;

import java.util.Optional;

import java.util.Properties;


public class YmlPropertyResourceFactory implements PropertySourceFactory {


@Override

public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {

String resourceName = Optional.ofNullable(name).orElse(encodedResource.getResource().getFilename());


if(resourceName.endsWith(".yml") || resourceName.endsWith(".yaml")) {

List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, encodedResource.getResource());

return yamlSources.get(0);

} else {

return new PropertiesPropertySource(resourceName, new Properties());

}

}

}



四、编写Person类和person.yml 配置文件

写个bean类进行属性映射,注意替换一下默认的factory参数,factory =
YmlPropertyResourceFactory.class。


@Component组件

注入Spring容器,只有Spring容器中的组件才能使用@ConfigurationProperties功能;


@PropertySource(value = "classpath:person.yml",encoding = "utf-8",factory = YmlPropertyResourceFactory.class)

加载指定的配置文件;获取的是person.properties 这个配置文件下的配置信息;

factory =
YmlPropertyResourceFactory.class 设置使用自定义的配置文件读取的工厂类;


@ConfigurationProperties(prefix = "person")

告诉SpringBoot将本类中的相关配置与yml文件中的配置绑定,并获取配置前缀person下的配置项。


下面是person.yml 配置文件,同时将application.yml 中的person配置项注释掉。



五、测试验证

编写单元测试类,进行测试验证。

测试结果如下所示 :



六、扩展PropertyResourceFactory 工厂类

如果既要支持原来的yml,又要支持properties,就可以将PropertyResourceFactory类进行改写一下。

package com.rickie.springboot.core;


import org.springframework.boot.env.YamlPropertySourceLoader;

import org.springframework.core.env.PropertySource;

import org.springframework.core.io.support.DefaultPropertySourceFactory;

import org.springframework.core.io.support.EncodedResource;

import org.springframework.core.io.support.PropertySourceFactory;

import org.springframework.lang.Nullable;


import java.io.IOException;

import java.util.List;

import java.util.Optional;


public class CommonPropertyResourceFactory implements PropertySourceFactory {

/**

* Create a {@link PropertySource} that wraps the given resource.

*

* @param name the name of the property source

* @param resource the resource (potentially encoded) to wrap

* @return the new {@link PropertySource} (never {@code null})

* @throws IOException if resource resolution failed

*/

@Override

public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {

String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename());

if (resourceName.endsWith(".yml") || resourceName.endsWith(".yaml")) {

List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, resource.getResource());

return yamlSources.get(0);

} else {

return new DefaultPropertySourceFactory().createPropertySource(name, resource);

}

}

}

这个类就可以支持原来的properties文件,也可以支持yml文件。

调用的时候,要改一下factory参数:

@PropertySource(value = "classpath:person.yml",encoding = "utf-8",factory = CommonPropertyResourceFactory.class)


还可以编写Rest方法进行验证,如下所示:

运行时返回JSON信息如下所示:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表