Search Tutorials


Use YAML properties in Spring Boot | JavaInUse



Use YAML properties in Spring Boot

YAML is a human-readable data serialization language.
It is commonly used for configuration files. In all the examples that we used in the Spring Boot Tutorials we made use of application.properties. But this has certain disadvantages as regards to the ease of use.

Configuration file without YAML

For example suppose we are defining configuration for the actuator endpoints for a Spring Boot Application.
info.app.name=testApp
info.app.description=Application for retrieving data.
info.app.version=@project.version@
info.app.build-number=@build.number@
If over time more information is to be added, this will keep on becoming more complex because of the .(dot) structure used here.

Advantages of using YAML for configuration

The above Actuator configuration can be written using YAML as follows
info:
    app:
        build-number: '@build.number@'
        description: Application for retrieving data.
        name: testApp
        version: '@project.version@'
Here we can see that YAML file is much more structured and less confusing in case we want to add complex properties in the configuration file. As can be seen YAML has hierarchical configuration data.
Also suppose we want to fetch the properties from the YAML file this can be easily done using an Iterator-
@Value("{''.split(',')}")
private List<String> infoConfigurations;
We can now iterate over this List and get the properties.