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:

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:

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:

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

Let’s now define our AppConfig class:

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:

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 *