UPDATE:
As the behaviour of spring.config.location now overrides the default instead of adding to it. You need to use spring.config.additional-location
to keep the defaults. This is a change in behaviour from 1.x to 2.x
When using Spring Boot the properties are loaded in the following order (see Externalized Configuration in the Spring Boot reference guide).
- Command line arguments.
- Java System properties (System.getProperties()).
- OS environment variables.
- JNDI attributes from java:comp/env
- A RandomValuePropertySource that only has properties in random.*.
- Application properties outside of your packaged jar (application.properties including YAML and profile variants).
- Application properties packaged inside your jar (application.properties including YAML and profile variants).
- @PropertySource annotations on your @Configuration classes.
- Default properties (specified using SpringApplication.setDefaultProperties).
When resolving properties (i.e. @Value("${myprop}")
resolving is done in the reverse order (so starting with 9).
To add different files you can use the spring.config.location
properties which takes a comma separated list of property files or file location (directories).
-Dspring.config.location=your/config/dir/
The one above will add a directory which will be consulted for application.properties
files.
-Dspring.config.location=classpath:job1.properties,classpath:job2.properties
This will add the 2 properties file to the files that are loaded.
The default configuration files and locations are loaded before the additonally specified spring.config.location
ones meaning that the latter will always override properties set in the earlier ones. (See also this section of the Spring Boot Reference Guide).
If
spring.config.location
contains directories (as opposed to files) they should end in / (and will be appended with the names generated fromspring.config.name
before being loaded). The default search pathclasspath:,classpath:/config,file:,file:config/
is always used, irrespective of the value ofspring.config.location
. In that way you can set up default values for your application inapplication.properties
(or whatever other basename you choose withspring.config.name
) and override it at runtime with a different file, keeping the defaults.