@PropertySource with YAML Files in Spring Boot – 在Spring Boot中使用YAML文件的@PropertySource

最后修改: 2020年 5月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll show how to read a YAML properties file using the @PropertySource annotation in Spring Boot.

在这个快速教程中,我们将展示如何使用Spring Boot中的@PropertySource注释来读取YAML属性文件。

2. @PropertySource and YAML Format

2.@PropertySourceYAML格式

Spring Boot has great support for externalized configuration. Also, it’s possible to use different ways and formats to read the properties in the Spring Boot application out-of-the-box.

Spring Boot对外部化配置有很大支持。此外,还可以使用不同的方式和格式来读取Spring Boot应用程序中开箱即用的属性。

However, by default, @PropertySource doesn’t load YAML files. This fact is explicitly mentioned in the official documentation.

然而,默认情况下,@PropertySource不会加载YAML文件。这一事实在官方文档中被明确提及。

So, if we want to use the @PropertySource annotation in our application, we need to stick with the standard properties files. Or we can implement the missing puzzle piece ourselves!

因此,如果我们想在我们的应用程序中使用@PropertySource注释,我们需要坚持使用标准properties文件。或者我们可以自己实现缺少的那块拼图!

3. Custom PropertySourceFactory

3.自定义PropertySourceFactory

As of Spring 4.3, @PropertySource comes with the factory attribute. We can make use of it to provide our custom implementation of the PropertySourceFactory, which will handle the YAML file processing.

从Spring 4.3开始,@PropertySource自带factory属性。我们可以利用它来提供我们对PropertySourceFactory的自定义实现,它将处理YAML文件的处理

This is easier than it sounds! Let’s see how to do this:

这比听起来要容易得多!让我们看看如何做到这一点:

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) 
      throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}

As we can see, it’s enough to implement a single createPropertySource method.

正如我们所看到的,实现一个单一的createPropertySource方法即可。

In our custom implementation, first, we used the YamlPropertiesFactoryBean to convert the resources in YAML format to the java.util.Properties object.

在我们的自定义实现中,首先,我们使用了YamlPropertiesFactoryBean 将YAML格式的资源转换成java.util.Properties对象

Then, we simply returned a new instance of the PropertiesPropertySource, which is a wrapper that allows Spring to read the parsed properties.

然后,我们简单地返回了一个新的PropertiesPropertySource的实例,它是一个包装器,允许Spring读取解析的属性。

4. @PropertySource and YAML in Action

4.@PropertySourceYAML的作用

Let’s now put all the pieces together and see how to use them in practice.

现在让我们把所有的碎片放在一起,看看如何在实践中使用它们。

First, let’s create a simple YAML file – foo.yml:

首先,让我们创建一个简单的YAML文件 – foo.yml

yaml:
  name: foo
  aliases:
    - abc
    - xyz

Next, let’s create a properties class with @ConfigurationProperties and use our custom YamlPropertySourceFactory:

接下来,让我们用@ConfigurationProperties创建一个属性类,并使用我们自定义的YamlPropertySourceFactory:

@Configuration
@ConfigurationProperties(prefix = "yaml")
@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class)
public class YamlFooProperties {

    private String name;

    private List<String> aliases;

    // standard getter and setters
}

And finally, let’s verify that the properties are properly injected:

最后,让我们验证一下属性是否被正确注入

@RunWith(SpringRunner.class)
@SpringBootTest
public class YamlFooPropertiesIntegrationTest {

    @Autowired
    private YamlFooProperties yamlFooProperties;

    @Test
    public void whenFactoryProvidedThenYamlPropertiesInjected() {
        assertThat(yamlFooProperties.getName()).isEqualTo("foo");
        assertThat(yamlFooProperties.getAliases()).containsExactly("abc", "xyz");
    }
}

5. Conclusion

5.总结

To sum up, in this quick tutorial, we first showed how easy it is to create a custom PropertySourceFactory. After that, we presented how to pass this custom implementation to the @PropertySource using its factory attribute.

总之,在这个快速教程中,我们首先展示了创建一个自定义的PropertySourceFactory是多么简单。之后,我们介绍了如何使用其@PropertySource属性将这个自定义实现传递给

Consequently, we were able to successfully load the YAML properties file into our Spring Boot application.

因此,我们能够成功地将YAML属性文件加载到我们的Spring Boot应用程序中

As usual, all the code examples are available over on GitHub.

像往常一样,所有的代码实例都可以在GitHub上找到