Spring Boot Externalized Configuration
Spring Boot uses a very particular PropertySource
order that is designed to allow sensible overriding of values. Properties are considered in the following order (with values from lower items overriding earlier ones):
- Default properties (specified by setting
SpringApplication.setDefaultProperties
). @PropertySource
annotations on your@Configuration
classes. Please note that such property sources are not added to theEnvironment
until the application context is being refreshed. This is too late to configure certain properties such aslogging.*
andspring.main.*
which are read before refresh begins.- Config data (such as
application.properties
files).- Application properties packaged inside your jar (
application.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). - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants).
- Application properties packaged inside your jar (
- A
RandomValuePropertySource
that has properties only inrandom.*
. - OS environment variables.
- Java System properties (
System.getProperties()
). - JNDI attributes from
java:comp/env
. ServletContext
init parameters.ServletConfig
init parameters.- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property). - Command line arguments.
properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.@TestPropertySource
annotations on your tests.- Devtools global settings properties in the
$HOME/.config/spring-boot
directory when devtools is active.
Config data
Specify a default configuration file
spring.config.location
: specify a default configuration file path or directory
java -jar myproject.jar --spring.config.location=\ |
mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties" |
Importing Additional Configuration File
spring.config.additional-location
: additional configuration file that will override default configuration file
Program arguments
java -jar your_app.jar --spring.config.additional-location=xxx |
System Properties (VM Arguments)
java -jar -Dspring.config.additional-location=xxx your_app.jar |
application.properties
spring.config.import=developer.properties |
OS Environment Variables
If you add new OS Environment Variables on Windows, you must restart your processes (Java process, Intellij IDEA) to read the new OS Environment Variables.
For any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.
Add User variables or System variables on Linux or Windows
msg=hello |
- Read by System Class
System.getenv("msg") |
- Read by Environment object
|
- Injecting environment variables
|
- Setting
application.properties
values from environment
msg=${msg} |
JSON Application Properties
Environment variables and system properties often have restrictions that mean some property names cannot be used. To help with this, Spring Boot allows you to encode a block of properties into a single JSON structure.
When your application starts, any spring.application.json
or SPRING_APPLICATION_JSON
properties will be parsed and added to the Environment
.
For example, the SPRING_APPLICATION_JSON
property can be supplied on the command line in a UN*X shell as an environment variable:
SPRING_APPLICATION_JSON='{"my":{"name":"test"}}' java -jar myapp.jar |
In the preceding example, you end up with my.name=test
in the Spring Environment
.
The same JSON can also be provided as a system property:
java -Dspring.application.json='{"my":{"name":"test"}}' -jar myapp.jar |
Or you could supply the JSON by using a command line argument:
java -jar myapp.jar --spring.application.json='{"my":{"name":"test"}}' |
If you are deploying to a classic Application Server, you could also use a JNDI variable named java:comp/env/spring.application.json
.
Accessing Command Line Properties
By default, SpringApplication
converts any command line option arguments (that is, arguments starting with --
, such as --server.port=9000
) to a property
and adds them to the Spring Environment
. As mentioned previously, command line properties always take precedence over file-based property sources.
If you do not want command line properties to be added to the Environment
, you can disable them by using SpringApplication.setAddCommandLineProperties(false)
.
Maven Command with application arguments:
mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value" |
$ mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=production --server.port=8089" |
# Spring-Boot 2.x |
java -jar
with System Properties (VM Arguments)
java -jar -D{arg.name}={value} {jar-file} // sometimes value need add quotes "" |
$ mvn clean install spring-boot:repackage -Dmaven.test.skip=true |
java -jar
with Program Arguments
java -jar <jar-file> --{arg.name}={value} // sometimes value need add quotes "" |
$ mvn clean install spring-boot:repackage -Dmaven.test.skip=true |