Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precedence order among properties file, YAML file, and Command Line arguments in SpringBoot

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.

like image 213
Ram Avatar asked Aug 22 '17 16:08

Ram


People also ask

In what order the properties are loaded in spring boot?

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order: Command line arguments. JNDI attributes from java:comp/env . Java System properties ( System.

Which property is given precedence in spring?

Spring Boot application converts the command line properties into Spring Boot Environment properties. Command line properties take precedence over the other property sources. By default, Spring Boot uses the 8080 port number to start the Tomcat.

Which property file will be loaded first by spring boot?

If your application package contains the application. properties, Spring Boot will load properties from the external file with higher priority.

Which of the following are the priority orders of configuration for spring boot application?

After all user beans have been created. Spring Boot auto configuration classes almost always use the @ConditionalXXXX annotations to make sure that any beans of the same type/name and other conditions that are configured in your application will take precedence over the Spring Boot auto-configured beans.


2 Answers

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).
like image 124
glytching Avatar answered Oct 01 '22 07:10

glytching


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.

like image 43
spandey Avatar answered Oct 01 '22 07:10

spandey