application.yml vs application.properties for Spring Boot
Solution 1
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
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
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
Related videos on Youtube
Comments
-
cupakob over 2 years
In my project i'm currently using
application.yml
for configuration. Spring Initializr generateapplication.properties
? What are the Pro/Cons for each one? -
Bruce Sun over 5 yearsIs YAML configuration format widely accepted across the whole SpringBoot world? When we come to a 3rd party library that provides only properties format configuration, is it easy to convert it to yaml format ourselves?
-
masterxilo over 5 years@BruceSun It seems not so accepted, because official documentation uses it rarely, this uses .properties for instance: docs.spring.io/spring-boot/docs/current/reference/html/… But my company's spring boot project template has yml...
-
Nurlan over 5 yearsThey are not just different data formats. YAML supports utf-8, application.properties doesn't
-
David Fariña almost 5 yearsAnother question on this topic: When a library requires me to set a configuration to the application.yml file, can I assume that it can be loaded when I just set the property in the application.properties file? To be precise, is it the same mechanism to load a property for both files?
-
Erik Kaplun almost 5 yearsalso, what happens when my code base provides both an
application.yaml
as well asapplication.properties
? can I just move the stuff in, say, the.yaml
one into.properties
(changing format as required) and expect things to continue to work the same way? -
ahmednabil88 almost 4 yearsGood answer, just remarks about point#2
.properties file
: can supports key-value pair as well as map, list & scalar type values. + point#3.properties file
can be used with any language -
Stunner over 3 yearsso we can have either application.yml or application.properties.. one is enough or both files required?
-
Stunner over 3 yearsso we can have either application.yml or application.properties.. one is enough or both files required? if we keep both files , what is the impact , which one will be picked up
-
Dhwanil Patel over 3 years@Stunner It's completely depends on your requirements. But you can use both of them if they meet with your need. Example, I worked in one project and in that common properties like database configuration, cache configuration, mail configuration defined in application.properties and for device specific configuration we create different ymls.
-
Stunner over 3 yearsso u mean to say , no matter how many properties file or yaml files declared or even the combination of both properties file and yaml file, the spring doesn't mind the number and loads all of them and both of them?
-
Dhwanil Patel over 3 yearsNo not exact that, Let's comes bit backward, Why properties or yml are used? Answer is for profiling and manage external properties. So not any professional developer create un-appropriate n number of files. In short have to create properties and yml file based on your number profile or some data which you want to pass as external data.
-
Dhwanil Patel over 3 yearsFor second question, Spring not load all the profiles it load according to your need. Like active profile parameter or based on profile parameter you passed in environment variable or command line option.
-
Dhwanil Patel over 3 yearsI suppose you have bit confuse regarding properties or yml file define inside project and pass externally with command line param. Is it so? If you have some specific context then start chat or place question regarding that. Have a nice day :)