Spring Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Introduction

YAML is a human-friendly notation used in configuration files. Why would we prefer this data serialization over the properties file in Spring Boot? Besides readability and reduction of repetition, YAML is the perfect language to write Configuration as Code for the deployments.

In the same way, the use of YAML for Spring DevOps facilitates the storage of the configuration variables in the environment as the 12 Factor Authenticator recommends.

In this tutorial, we'll compare Spring YAML versus properties file in order to check the main advantages of using one over the other. But remember, the selection of YAML over properties file configuration is sometimes a decision of personal taste.

2. YAML Notation

YAML stands for a recursive acronym for “YAML Ain't Markup Language“. It provides the following characteristics:

  • More clarity and human-friendliness
  • Perfect for hierarchical configuration data
  • It supports enhance capabilities such as maps, lists, and scalar types

Those capabilities make YAML the perfect companion for Spring configuration files.  A word of caution here for those starting out with YAML: writing it can be a bit tedious at the beginning due to its indentation rules.

Let's see how it works!

3. Spring YAML Configuration

As it was mentioned in the previous sections, YAML is an extraordinary data format for configuration files. It's much more readable, and it provides enhanced capabilities over the properties file. Therefore, it makes sense to recommend this notation over the properties file configuration. Furthermore, from version 1.2, YAML is a superset of JSON.

In addition, in Spring the configuration files placed outside the artifact override those inside the packaged jar. Another interesting feature of Spring configuration is the possibility to assign environment variables at runtime. This is extremely important for DevOps deployments.

Spring profiles allow separating the environments and apply different properties to them. YAML adds the possibility to include several profiles in the same file.

In our case, for deployment purposes, we'll have three: testing, development, and production:

    - test


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

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

    profiles: dev
name: ${DEV_NAME:dev-YAML}
environment: development
    - www.abc.dev.com
    - www.xyz.dev.com

Let's now check the spring.profiles.active property which assigns the test environment by default. We can redeploy the artifact using different profiles without building again the source code.

Another interesting feature in Spring is that you can enable the profile via the environment variable:


We'll see the relevance of this environment variable in the Testing section. Finally, we can configure YAML properties assigning directly the value from the environment:

name: ${DEV_NAME:dev-YAML}

We can see that if no environment variable is configured, a default value test-YAML is used.

4. Reduction of Repetition and Readability

The hierarchical structure of YAML provides ways of reducing the upper levels of the configuration properties file. Let's see the differences with an example:

    url: myurl
    user: user
    password: password
    description: >
      this should be a long 
    url: myurlservice
    token: token
    description: >
      this should be another long 

The same configuration would become redundant using properties file:

component.idm.description=this should be a long \
component.service.description=this should be another long \ 

The hierarchical nature of YAML greatly enhances legibility. It is not only a question of avoiding repetitions but also the indentation, well used, perfectly describes what the configuration is about and what is for. With YAML, as in the case of properties file with a backslash \, it is possible to break the content into multiple lines with > character.

5. Lists and Maps

We can configure lists and maps using YAML and properties file.

There are two ways to assign values and store them in a list:

  - www.abc.test.com
  - www.xyz.test.com
external: [www.abc.test.com, www.xyz.test.com]

Both examples provide the same result. The equivalent configuration using properties file would be more difficult to read:


external=www.abc.test.com, www.xyz.test.com

Again YAML version is more human-readable and clear.

In the same way, we can configure maps:

  firstkey: key1
  secondkey: key2

6. Testing

Now, let's check if everything is working as expected. If we check the logging of the application, we can see that the environment selected by default is testing:

2020-06-11 13:58:28.846  INFO 10720 --- [main] com.baeldung.yaml.MyApplication: ...
using environment:testing
servers:[www.abc.test.com, www.xyz.test.com]
external:[www.abc.test.com, www.xyz.test.com]
map:{firstkey=key1, secondkey=key2}
   Url: myurl
   User: user
   Password: password
   Description: this should be a long description

   Url: myurlservice
   Token: token
   Description: this should be another long description

We can overwrite the name by configuring DEV_NAME in the environment:

export DEV_NAME=new-dev-YAML

We can see that the name of the environment changes executing the application with dev profile:

2020-06-11 17:00:45.459  INFO 19636 --- [main] com.baeldung.yaml.MyApplication: ...
using environment:development
servers:[www.abc.dev.com, www.xyz.dev.com]

Let's run for the production environment using SPRING_PROFILES_ACTIVE=prod:


2020-06-11 17:03:33.074  INFO 20716 --- [main] ...
using environment:production
servers:[www.abc.com, www.xyz.com]

7. Conclusion

In this tutorial, we described the intricacies of the use of YAML configuration compared to the properties file.

We showed that YAML provides human friendliness capabilities, it reduces repetition and is more concise than its properties file variant.

As always, the code is available over on GitHub.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Newest Most Voted
Inline Feedbacks
View all comments
3 months ago

Yaml is good for reading but very bad for writing due to its whitespace sensitivity.

Loredana Crusoveanu
3 months ago
Reply to  Icke

Hi Icke,
That’s true! But it’s a matter of practice and a small price to pay for all the advantages you get.
Nevertheless, we’ll update the article to include this observation.

2 months ago

I ended up with tab character issue on production at the time of start up after deployment for prod yml file. Does anyone know how can we validate yml files for such issues, may be a good maven plugin?

Loredana Crusoveanu
2 months ago
Reply to  Anonymous

A good practice is to use an IDE extension or plugin that’ll validate your application.yml for you during development. For example, in Eclipse you can install the Spring Tools For Eclipse IDE from the marketplace.
Alternatively, there are online validators such as YAML Lint and maven plugins such as yaml-json-validator which can help you here.

Comments are closed on this article!