Spring YAML Configuration – Spring YAML配置

最后修改: 2017年 6月 22日

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

1. Overview

1.概述

One of the ways of configuring Spring applications is using YAML configuration files.

配置Spring应用程序的方法之一是使用YAML配置文件。

In this quick tutorial, we’ll configure different profiles for a simple Spring Boot application using YAML.

在这个快速教程中,我们将使用YAML为一个简单的Spring Boot应用程序配置不同的配置文件。

2. Spring YAML File

2.Spring YAML文件

Spring profiles help enable Spring Applications to define different properties for different environments.

Spring配置文件有助于使Spring应用程序为不同的环境定义不同的属性。

Let’s take a look at a simple YAML file that contains two profiles. The three dashes separating the two profiles indicate the start of a new document, so all the profiles can be described in the same YAML file.

让我们来看看一个包含两个配置文件的简单YAML文件。分隔两个配置文件的三个破折号表示一个新文件的开始,因此所有的配置文件可以在同一个YAML文件中描述。

The relative path of the application.yml file is /myApplication/src/main/resources/application.yml:

application.yml文件的相对路径是/myApplication/src/main/resources/application.yml:

spring:
    config:
        activate:
            on-profile: test
name: test-YAML
environment: testing
enabled: false
servers: 
    - www.abc.test.com
    - www.xyz.test.com

---
spring:
    config:
        activate:
            on-profile: prod
name: prod-YAML
environment: production
enabled: true
servers: 
    - www.abc.com
    - www.xyz.com

Note that this setup doesn’t imply that any of these profiles will be active when we start our application. The properties defined in the profile-specific documents won’t be loaded unless we explicitly indicate it; by default, the only active profile will be ‘default.

请注意,这种设置并不意味着当我们启动应用程序时,这些配置文件中的任何一个都会被激活。除非我们明确指出,否则在配置文件特定文件中定义的属性不会被加载;默认情况下,唯一活跃的配置文件将是’default.

3. Binding YAML to a Config Class

3.将YAML绑定到一个配置类

To load a set of related properties from a properties file, we will create a bean class:

为了从属性文件中加载一组相关属性,我们将创建一个bean类。

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {
 
    private String name;
    private String environment;
    private boolean enabled;
    private List<String> servers = new ArrayList<>();

    // standard getters and setters

}

The annotations used here are:

这里使用的注释是。

  • @Configurationthis marks the class as a source of bean definitions
  • @ConfigurationProperties – this binds and validates the external configurations to a configuration class
  • @EnableConfigurationProperties – this annotation is used to enable @ConfigurationProperties annotated beans in the Spring application

4. Accessing the YAML Properties

4.访问YAML属性

To access the YAML properties, we’ll create an object of the YAMLConfig class, and access the properties using that object.

为了访问YAML属性,我们将创建一个YAMLConfig类的对象,并使用该对象访问这些属性。

In the properties file, we’ll set the spring.profiles.active environment variable to prod. If we don’t define this property, only the ‘default’ profile will be active.

在属性文件中,我们将设置spring.profiles.active环境变量为prod。如果我们不定义这个属性只有‘default’配置文件将被激活。

The relative path for the properties file is /myApplication/src/main/resources/application.properties:

属性文件的相对路径是/myApplication/src/main/resources/application.properties:

spring.profiles.active=prod

In this example, we’ll display the properties using the CommandLineRunner:

在这个例子中,我们将使用CommandLineRunner显示属性:

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Autowired
    private YAMLConfig myConfig;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.run();
    }

    public void run(String... args) throws Exception {
        System.out.println("using environment: " + myConfig.getEnvironment());
        System.out.println("name: " + myConfig.getName());
        System.out.println("enabled:" + myConfig.isEnabled());
        System.out.println("servers: " + myConfig.getServers());
    }
}

The output on the command line:

命令行上的输出。

using environment: production
name: prod-YAML
enabled: true
servers: [www.abc.com, www.xyz.com]

5. YAML Property Overriding

5.YAML属性的重写

In Spring Boot, YAML files can be overridden by other YAML properties files.

在Spring Boot中,YAML文件可以被其他YAML属性文件所覆盖。

Prior to version 2.4.0, YAML properties were overridden by properties files in the following locations, in order of highest precedence first:

在2.4.0版本之前,YAML属性被以下位置的属性文件所覆盖,按照优先级最高的顺序排列。

  • Profiles’ properties placed outside the packaged jar
  • Profiles’ properties packaged inside the packaged jar
  • Application properties placed outside the packaged jar
  • Application properties packaged inside the packaged jar

As of Spring Boot 2.4, external files always override packaged files, regardless of whether they’re profile-specific or not.

从Spring Boot 2.4开始,外部文件总是凌驾于打包的文件之上,无论它们是否是特定的配置文件。

6. Conclusion

6.结论

In this brief article, we learned how to configure properties in Spring Boot applications using YAML. We also discussed the property overriding rules followed by Spring Boot for YAML files.

在这篇简短的文章中,我们学习了如何使用YAML在Spring Boot应用程序中配置属性。我们还讨论了Spring Boot对YAML文件遵循的属性覆盖规则。

The code for this article is available over on GitHub.

本文的代码可在GitHub上获得