Spring Boot applications support YAML as an alternative to the application.properties file. YAML isn’t a markup language but it allows you to define properties in the hierarchical configuration.
The parser for YAML is called SnakeYAML and it is automatically added to the classpath by spring-boot-starters so you don’t have to configure anything extra to support YAML is quite convenient if you use a hierarchical configuration data. Spring Boot properties are organized in groups, for example, server, database, and so on.
Let’s see an exanmple from application.properties and how this can be turned into YAML:
database.host = localhost database.user = admin
In application.yml :
database: host: localhost user: admin
Using YAML configuration for defining profiles
The main advantage of a YAML file is that it can contain configuration for multiple profiles in a single YAML file. Spring Boot provides a spring.profiles key to indicate when the document applies. Let’s see the following example of how to define multiple profile-specific configurations in a single YAML file:
#Used for all profiles logging.level: org.springframework: INFO #'dev' profile only spring.profiles: dev database: host: localhost user: dev #'prod' profile only spring.profiles: prod database: host: 192.168.200.109 user: admin
In this application.yml file, we have defined database settings according to two profiles, dev and prod , by using a spring.profile key. In the file, ‘—‘ implies a separation between profiles.
In case you have both application.properties and application.yml in the same application, application.yml will override the settings defined in application.properties .