问题
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.
回答1:
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:
- Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
- @TestPropertySource annotations on your tests.
- @SpringBootTest#properties annotation attribute on your tests.
- Command line arguments.
- Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
- ServletConfig init parameters.
- ServletContext init parameters.
- JNDI attributes from java:comp/env.
- Java System properties (System.getProperties()).
- OS environment variables.
- A RandomValuePropertySource that only has properties in random.*.
- Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
- Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
- Application properties outside of your packaged jar (application.properties and YAML variants).
- Application properties packaged inside your jar (application.properties and YAML variants).
- @PropertySource annotations on your @Configuration classes.
- Default properties (specified using SpringApplication.setDefaultProperties).
回答2:
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.
来源:https://stackoverflow.com/questions/45822326/precedence-order-among-properties-file-yaml-file-and-command-line-arguments-in