In my project i'm currently using application.yml
for configuration. Spring Initializr generate application.properties
? What are the Pro/Cons for each one?
4 Answers
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.
-
4Is 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? Aug 19, 2018 at 3:17
-
1@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... Nov 21, 2018 at 9:34
-
4They are not just different data formats. YAML supports utf-8, application.properties doesn't– NurlanNov 29, 2018 at 7:51
-
1Another 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? Jun 20, 2019 at 14:55
-
4also, 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? Jun 24, 2019 at 12:35
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
.
-
1Good 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 Jul 20, 2020 at 23:16 -
so we can have either application.yml or application.properties.. one is enough or both files required?– StunnerJan 22, 2021 at 3:51
-
so 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– StunnerJan 22, 2021 at 3:57
-
@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. Jan 22, 2021 at 4:25
-
so 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?– StunnerJan 22, 2021 at 4:33
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
application.properties
It uses a simple key-value pair format.
Properties are defined using the key=value syntax.
Properties can be written in a single line or multiple lines, but each property is represented by a single key-value pair.
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.show_sql=true
application.yml
It uses a YAML (YAML Ain’t Markup Language) format.
Properties are defined using indentation and hierarchical structure.
Properties can be written in a more structured manner using indentation and nesting.
YAML supports lists and maps, making it easier to represent complex configurations.
server: port: 8080
spring: datasource: password: root url: jdbc:mysql://localhost:3306/demo username: root jpa: hibernate: ddl-auto: update properties: hibernate: show_sql: true
Reference: spring boot properties vs. yaml