application.yml vs application.properties for Spring Boot

77,132

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:

  1. .properties stores data in sequential format, whereas
    .yml stores data in hierarchical format.

  2. .properties supports only key-value pairs (basically string values), whereas
    .yml supports key-value pair, as well as map, list & scalar type values.

  3. .properties is specifically used by Java, whereas
    .yml can be used by other languages (eg Java, Python, ROR, etc).

  4. 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.

  5. 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

Share:
77,132

Related videos on Youtube

cupakob
Author by

cupakob

CleanCode- und OpenSource/Linux-Enthusiast

Updated on September 24, 2021

Comments

  • cupakob
    cupakob over 2 years

    In my project i'm currently using application.yml for configuration. Spring Initializr generate application.properties? What are the Pro/Cons for each one?

  • Bruce Sun
    Bruce Sun over 5 years
    Is 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
    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
    Nurlan over 5 years
    They are not just different data formats. YAML supports utf-8, application.properties doesn't
  • David Fariña
    David Fariña almost 5 years
    Another 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
    Erik Kaplun almost 5 years
    also, what happens when my code base provides both an application.yaml as well as application.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
    ahmednabil88 almost 4 years
    Good 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
    Stunner over 3 years
    so we can have either application.yml or application.properties.. one is enough or both files required?
  • Stunner
    Stunner over 3 years
    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
  • Dhwanil Patel
    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
    Stunner over 3 years
    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?
  • Dhwanil Patel
    Dhwanil Patel over 3 years
    No 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
    Dhwanil Patel over 3 years
    For 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
    Dhwanil Patel over 3 years
    I 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 :)