First, Introduction
Spring Boot project application.properties file storage and usage introduction
Second, the method of a multi-environment configuration file
We generally have a number of application environments, development environment, test environment, production environment, the configuration of each environment will be slightly different, I can create multiple profiles based on this, the master configuration file to control the read that sub-configuration
After you create a spring boot project, you can create more than one. properties file at the same time, as long as it meets the format it requires
Format: Application-{profile}.properties;{profile} is a variable used to customize the profile name
Create a configuration and a master configuration for three application environments, respectively
1, application.properties master configuration (The following is the configuration content, where Dev is the other profile name of Dev, test, prod)
# The identity name (format: application-{profile}.properties;{profile} is the variable used to customize the profile name) for the specific configuration filespring.profiles.active= Dev
2. application-dev.properties development environment (configuration content below)
spring.application.name=tyh-demo-prop# development environment Port server.port=10001
3. application-test.properties test Environment (Below is the configuration content)
spring.application.name=tyh-demo-prop# test environment Port server.port=10002
4. application-prod.properties Production Environment (Below is the configuration content)
spring.application.name=tyh-demo-prop# production environment Port server.port=10003
Change the Spring.profiles.active=dev in the master configuration to switch between the different sub-profiles.
Because of this method. The properties file is still in the jar, we are not easy to modify, and too much information is exposed in the development of easy to leak, so the combination of method II for use
Iii. Method Two jar package external configuration file
We often modify the content of the configuration file when we develop the production environment, and the default. The properties configuration file is not easily modified in the jar package, so spring boot gives you a few places to read the configuration file. We can use this method to modify the configuration file from outside the jar package.
In general, we will place the. Properties in the Resources folder
Spring boot will look for the configuration file in the following order
1, "current directory" under the/config folder
2. Under "Current directory"
3, classpath the/config folder
4. Under Classpath
The following is a legend explanation:
When the configuration file is found, it will not continue to look for, it is said that the file priority below the configuration file will not be read, found to stop
"Current directory" means that after we hit the executable jar package, we usually start with a bat file, which refers to the directory of the bat file.
Our regular location is the lowest priority, so we only need to copy a copy of the configuration file, placed in the bat's "current directory" can be
Spring Boot Project application.properties Multi-environment configuration file, jar package external configuration file