Spring Boot YAML Configuration


In this quick tutorial, we’ll learn how to use a YAML file to configure properties of a Spring Boot application.

What is YAML File?

Instead of having an application.properties in Spring, we can use the application.yml as our configuration file. YAML is a superset of JSON and we can use it for configuring data. The YAML files are more human-readable, especially when we have a lot of hierarchical configurations in place.

Let’s see what a very basic YAML file looks like:

    url: http://localhost  
        name: MyApplication
        threadCount: 4

The above YAML file is equivalent to the below application.properties file:


Spring uses SnakeYAML for parsing the YAML file, which is available in spring-boot-starter:


We can check out the latest version of this dependency at Maven Repository.

Spring Profiles In YAML:

We can use the spring.profiles key to mention the profile for which a property value applies. For example:

    profiles: dev | test
    url: http://localhost  
        name: MyApplication
        threadCount: 4
          - A
          - B
    profiles: prod
    url: http://myapp.org 
        name: MyApplication
        threadCount: 10
          - Jacob
          - James

The property values are then assigned based on the active spring profile. While running the Spring application, we can set the profile as:


Binding YAML Configuration:

One way to access YAML properties is to use the @Value(“${property}”) annotation. However, there’s another popular method that ensures that the strongly typed beans govern and validate our app configuration.

To implement that, we’ll create a @ConfigurationProperties class which maps a set of related properties:

public class ServerProperties {

    private String url;
    private final App app = new App();

    public App getApp() {
        return app;
    //getter and setter for url

    public static class App {

        private String name;
        private String threadCount;
        private List<String> users = new ArrayList<>();

        //getters and setters

Note that we can create one or more of @ConfigurationProperties classes.

Let’s now define our AppConfig class:

public class ApplicationConfig {



Here, we have mentioned the list of property classes to register in the @EnableConfigurationProperties annotation.

Accessing YAML Properties:

We can now access the YAML properties by making use of the @ConfigurationProperties beans that we have created. We’ll inject these property beans just like any regular Spring bean:

public class AppService {

    private ServerProperties config;

    public void printConfigs() {

We can then use the AppRunner to boot our Spring application and invoke our printConfigs() method. Our app will print out the property values depending on the active spring profile.


In this tutorial, we learned how to use YAML configuration files in Spring Boot application.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *