Contents
  1. 1. Config data
    1. 1.1. Specify a default configuration file
    2. 1.2. Importing Additional Configuration File
  2. 2. OS Environment Variables
  3. 3. JSON Application Properties
  4. 4. Accessing Command Line Properties
  5. 5. References

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

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  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).
    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).
  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. @TestPropertySource annotations on your tests.
  14. 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

1
2
3
java -jar myproject.jar --spring.config.location=\
optional:classpath:/default.properties,\
optional:classpath:/override.properties
1
2
3
4
mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"
mvn spring-boot:run -Dspring.config.location="file:///D:/config/aliyun-oss-java/application.yml"
mvn spring-boot:run -Dspring.config.location="/Users/home/jdbc.properties"
mvn spring-boot:run -Dspring.config.location="D:/config/aliyun-oss-java/application.yml"

Importing Additional Configuration File

spring.config.additional-location: additional configuration file that will override default configuration file

Program arguments

1
java -jar your_app.jar --spring.config.additional-location=xxx

System Properties (VM Arguments)

1
java -jar -Dspring.config.additional-location=xxx your_app.jar

application.properties

1
spring.config.import=developer.properties

OS Environment Variables

On Windows, if you add new OS Environment Variables, you must to restart process (Java process, Intellij IDEA) to read the new OS Environment Variables.

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

1
msg=hello
  1. Read by System Class
1
System.getenv("msg")
  1. Read by Environment object
1
2
3
4
@Autowired
private Environment environment;

environment.getProperty("msg")
  1. Injecting environment variables
1
2
@Value("${msg}")
private String msg;
  1. Setting application.properties values from environment
1
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:

1
$ 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:

1
$ java -Dspring.application.json='{"my":{"name":"test"}}' -jar myapp.jar

Or you could supply the JSON by using a command line argument:

1
$ 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:

1
2
mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"
mvn spring-boot:run -D{arg.name}={value}
1
2
$ mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=production --server.port=8089"
$ mvn spring-boot:run -Dspring-boot.run.arguments="--spring.config.location=D:\config\aliyun-oss-java\application.yml"
1
2
3
4
# Spring-Boot 2.x
$ mvn spring-boot:run -Dspring-boot.run.profiles=local
# Spring-Boot 1.x and 2.x
$ mvn spring-boot:run -Dspring.profiles.active=local

java -jar with System Properties (VM Arguments)

1
java -jar -D{arg.name}={value} {jar-file} // sometimes value need add quotes ""
1
2
3
$ mvn clean install spring-boot:repackage -Dmaven.test.skip=true
$ java -jar -Dspring.config.location="D:\config\aliyun-oss-java\application.yml" your_application.jar
$ java -jar -Dspring.profiles.active=prod your_application.jar

java -jar with Program Arguments

1
java -jar <jar-file> --{arg.name}={value} // sometimes value need add quotes ""
1
2
$ mvn clean install spring-boot:repackage -Dmaven.test.skip=true
$ java -jar your_application.jar --spring.config.location="D:\config\aliyun-oss-java\application.yml"

References

Contents
  1. 1. Config data
    1. 1.1. Specify a default configuration file
    2. 1.2. Importing Additional Configuration File
  2. 2. OS Environment Variables
  3. 3. JSON Application Properties
  4. 4. Accessing Command Line Properties
  5. 5. References