42

I have been using application.properties files since long in my Spring application. But recently I came across application.yaml files. What is the precedence order among all three and advantage (if there is one) of using individual.

I know this might be silly question. but I am confused with their usages.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ram
  • 3,887
  • 4
  • 27
  • 49
  • hope you are clear now with my answer. In simple words, if you have yaml and properties file both and same key in both, then spring boot will look first in properties and if not found then will go to yaml. – spandey Jun 19 '20 at 09:46

3 Answers3

64

Spring Boot property resolution property order is described here.

Use of application.properties and application.yaml is not expected. Use one format or the other but not both. Whichever one you use will be handled at position 12 or 13 (depending on whether the file is packaged in the application JAR or not) in property precedence order.

Including an extract from the above link here to avoid link rot ...

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that only has properties in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes.
  17. Default properties (specified using SpringApplication.setDefaultProperties).
Community
  • 1
  • 1
glytching
  • 44,936
  • 9
  • 114
  • 120
  • 2
    Thanks @Glitch, you definitely made it so clear. It's really helpful. You explained more than what I was expecting. – Ram Aug 22 '17 at 18:35
  • @MAC Glad it helped – glytching Aug 22 '17 at 18:57
  • 1
    The Spring Boot docs mention the precedence order in 2021: "It is recommended to stick with one format for your entire application. If you have configuration files with both .properties and .yml format in the same location, .properties takes precedence." As mentioned above, command-line supplied properties take precedence over both. – Jim Tough Jun 06 '21 at 17:03
21

In simple words,

if you have yaml and properties file both and same key in both, then spring boot will look first in properties and if not found then will go to yaml.

spandey
  • 1,034
  • 1
  • 15
  • 30
  • 4
    Although the accepted answer is correct, this fact is actually missing – Tarnschaf Jan 24 '19 at 08:33
  • thanks @Tarnschaf really not sure about reason .. i feel first precedence has been given to properties file. – spandey Jan 24 '19 at 09:20
  • 2
    Yes, we are using both (yml is in Git and we use git-ignored properties to store our personal database credentials), and it seems to work like you described. – Tarnschaf Jan 24 '19 at 17:24
  • What happens if I have two properties files by the same name (e.g. `abc.properties`) that contains an identically named attribute? – Anthony Kong Dec 13 '21 at 21:57
  • 1
    we cant have 2 prop files with same name at same location @AnthonyKong – spandey Dec 14 '21 at 09:18
0

I came here searching for the precedence of config files, specifically. To complement @glytching's answer, the precedence is simply for:

  • default then profile specific, and simultaneously
  • inside then outside.

For reference here is the quote from the current documentation focusing specifically on file precedence:

Config data files are considered in the following order:

  1. Application properties packaged inside your jar (application.properties and YAML variants).
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  3. Application properties outside of your packaged jar (application.properties and YAML variants).
  4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
Bahaa
  • 1,577
  • 18
  • 31