The new Certification Class of Learn Spring Security is out:

>> CHECK OUT THE COURSE

1. Overview

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

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

2. Spring YAML File

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

Following is 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.

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

The Spring application takes the first profile as the default profile unless declared otherwise in the Spring application.

spring:
    profiles: test
name: test-YAML
environment: test
servers: 
    - www.abc.test.com
    - www.xyz.test.com

---
spring:
    profiles: prod
name: prod-YAML
environment: production
servers: 
    - www.abc.com
    - www.xyz.com

3. Binding YAML to a Config Class

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

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

    // standard getters and setters

}

The annotation used here are:

  • @Configuration marks the class as a source of bean definitions
  • @ConfigurationProperties 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

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

In the properties file, let’s set the spring.active.profiles environment variable to prod. If we don’t define spring.profiles.active, it will default to the first profiles property defined in the YAML file.

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

spring.profiles.active=prod

In this example, we display the properties using the 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("servers: " + myConfig.getServers());
    }
}

The output on the command line:

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

5. YAML Property Overriding

In Spring Boot, YAML files can be overridden by other YAML properties files depending on their location. YAML properties can be overridden by properties files in the following locations, in order of highest precedence first:

  • 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

6. Conclusion

In this quick article, we’ve seen how to configure properties in Spring Boot applications using YAML. We’ve also seen the property overriding rules followed by Spring Boot for YAML files.

The code for this article is available over on GitHub.

Go deeper into Spring Security with the course:

>> LEARN SPRING SECURITY