spring-bootyamlcommand-line-argumentsintellij-idea-2016application.properties

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.


Solution

  • 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. Default properties (specified by setting SpringApplication.setDefaultProperties(Map)).
    2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
    3. Config data (such as application.properties files).
    4. A RandomValuePropertySource that has properties only in random.*.
    5. OS environment variables.
    6. Java System properties (System.getProperties()).
    7. JNDI attributes from java:comp/env.
    8. ServletContext init parameters.
    9. ServletConfig init parameters.
    10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    11. Command line arguments.
    12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
    13. @DynamicPropertySource annotations in your tests.
    14. @TestPropertySource annotations on your tests.
    15. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

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