application.yml vs application.properties for Spring Boot
Spring BootPropertiesConfigurationFormatYamlSpring Boot Problem Overview
In my project i'm currently using application.yml
for configuration. Spring Initializr generate application.properties
? What are the Pro/Cons for each one?
Spring Boot Solutions
Solution 1 - Spring Boot
Well, they are just different data formats. Which one's nicer and easier to read? That's obviously subjective. Here's a useful blog post.
As far as spring-boot
configuration is concerned, note that there's only one documented shortcoming of using YAML
. Per the documentation:
> YAML files can’t be loaded via the @PropertySource
annotation. So in the case that you need to load values that way, you need to use a properties file.
Solution 2 - Spring Boot
As per my knowledge, these are at least some of the differences:
-
.properties
stores data in sequential format, whereas
.yml
stores data in hierarchical format. -
.properties
supports only key-value pairs (basically string values), whereas
.yml
supports key-value pair, as well as map, list & scalar type values. -
.properties
is specifically used by Java, whereas
.yml
can be used by other languages (eg Java, Python, ROR, etc). -
When managing multiple configuration profiles, then:
.properties
requires you to create.properties
file per every profile, whereas in
.yml
you can create a section for each specific profile inside a single.yml
file. -
In Spring projects,
@PropertySource
annotation can only be used with.properties
.
Solution 3 - Spring Boot
One notable difference is how the properties are represented in each file. YAML files may use consistent spaces to denote hierarchy whereas properties file may use = to denote property values.
For ex.
Lists are represented hierarchically in YAML:
headers:
- user-agent
- x-wag-diagonalsize
Lists may be represented as inline list (separated by commas) in a properties file:
headers = user-agent, x-wag-diagonalsize
Another difference is we can add multiple configuration files into single yaml file.
For ex., we can add application.yaml(application specific properties) and bootstrap.yaml(server specific properties) into single config.yaml file