Home » Java » Enterprise Java » Spring Boot YAML Configuration

About Shubhra Srivastava

Shubhra Srivastava
Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)

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.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: Spring Boot YAML Configuration

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments