24.3 Application property files
SpringApplication
will load properties from application.properties
files in the following locations and add them to the Spring Environment
:
- A
/config
subdirectory of the current directory. - The current directory
- A classpath
/config
package - The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
Note | |
---|---|
You can also use YAML ('.yml') files as an alternative to '.properties'. |
If you don’t like application.properties
as the configuration file name you can switch to another by specifying a spring.config.name
environment property. You can also refer to an explicit location using the spring.config.location
environment property (comma-separated list of directory locations, or file paths).
$ java -jar myproject.jar --spring.config.name=myproject
or
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Warning | |
---|---|
spring.config.name and spring.config.location are used very early to determine which files have to be loaded so they have to be defined as an environment property (typically OS env, system property or command line argument). |
If spring.config.location
contains directories (as opposed to files) they should end in /
(and will be appended with the names generated from spring.config.name
before being loaded, including profile-specific file names). Files specified in spring.config.location
are used as-is, with no support for profile-specific variants, and will be overridden by any profile-specific properties.
The default search path classpath:,classpath:/config,file:,file:config/
is always used, irrespective of the value of spring.config.location
. This search path is ordered from lowest to highest precedence (file:config/
wins). If you do specify your own locations, they take precedence over all of the default locations and use the same lowest to highest precedence ordering. In that way you can set up default values for your application in application.properties
(or whatever other basename you choose with spring.config.name
) and override it at runtime with a different file, keeping the defaults.
Note | |
---|---|
If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (e.g. SPRING_CONFIG_NAME instead of spring.config.name ). |
Note | |
---|---|
If you are running in a container then JNDI properties (in java:comp/env ) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties. |