1. Overview
spring boot not only from a resource file folder or project under load configuration config folder, you can also load the configuration in the following locations:
- Command line parameters
- JNDI properties comp / env: From java
- Java system properties (System.getProperties ())
- Operating system environment variables
- random RandomValuePropertyResource configuration. * Property Value
- jar outer package or application- {profile} .properties application.yaml (with spring.profile) profile
- Internal jar package application- {profile} .properties or application.yaml (with spring.profile) profile
- jar or outer package application.properties application.yaml (without spring.profile) profile
- Internal jar package application.properties or application.yaml (without spring.profile) profile
- @PropertySource class notes on @Configuration
- By SpringApplication.setDefaultProperties specified default property
Wherein said marked in red part is we need to focus on mastering content, from top to bottom getting lower and lower-priority, high-priority configuration will overwrite low priority configuration, the configuration of high-priority and low-priority configuration will into effect, form a complementary configuration;
2. Example
2.1 command line parameters
To modify the start port number, for example, port number and we started jar can specify start for 9909
At this time we visited:
If you want to modify multiple configuration at boot time, in the middle can be separated by spaces, for example, we want to add a path to modify the port at the same time, you execute the following command:
java -jar spingboot01-1.0-SNAPSHOT.jar --server.port=9909 --server.servlet.context-path=/boot
Wherein: - server.port = 9909 is used to modify the port
--server.servlet.context-path = / boot increase access path is / boot, this time we want to execute the following results:
2.2jar outer bag and the inner bag jar
Remember the principle of loading: be looking outward from the jar package jar inside the bag, priority load the file with the profile, the file is loaded without a profile of;
Example: In this case we want to modify a plurality of such loading parameters, we can pull out a single configuration file in the same directory and jar package:
At this point if you start this jar sure the port number is 8801, the access path is / boot. At this point we place a configuration file in the same directory where the jar, port number 8888, access path is / hai;
At this point start the jar
At this point we visit: http: // localhost: 8888 / hai / hello