This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
The core philosophy of SpringBoot is to use convention over configuration to manage configuration information, allowing developers to run projects with little or no configuration required in their actual work.
SpringBoot configuration system
Profile
Profiles essentially represent a dimension for organizing configuration information. When a SpringBoot project needs to set up a set of configuration item templates, profiles can be created for each of these templates, in what we call the development, test, production, and other environments. To centrally manage configuration files, SpringBoot has certain conventions for naming configuration files. For example, label indicates the configuration version control information, and profile indicates the environment information corresponding to the configuration file. SpringBoot supports both.properties and.yml configuration file formats, and combines the concepts of label and profile. Therefore, the following configuration file names can be used in SpringBoot:
{application}.yml
{application}-{profile}.yml
{label}/{application}-{profile}.yml
{application}-{profile}.properties
{label}/{application}-{profile}.properties
Copy the code
SpringBoot Multi-environment configuration information
SpringBoot allows you to save all Profile configuration information in a single file. You only need to organize the information by Profile.
spring:
profiles: test
#test Environment configuration information
spring:
profiles: prod
#prod Environment configuration information
Copy the code
However, it is not recommended that you use this method to manage multiple profiles. Instead, you should use more flexible multiple profiles.
We only need to specify the Profile to be activated in the main Profile Application:
#properties
spring.profiles.active = test
#yml
spring:
profiles:
active: test
Activate multiple profiles simultaneously
spring.profiles.active: prod, myprofile1, myprofile2
Copy the code
If the active Profile is not specified in the main Profile, it can also be specified in the run command of the project Jar: Java -JAR application.jar –spring.profiles.active=prod, this implementation is often used in scenarios where packaging and deployment are automated through scripts.
Load sequence of configuration files
In a SpringBoot project, configuration files are allowed to be stored in multiple paths, and each path loads the configuration files in a different order. The SpringBoot project scans the application.properties or application.yml files in the following locations for global configuration files at startup.
// The configuration file in the config folder in the project path- file:. / config /// The configuration file in the project root path- the file: /// In the config folder of the project SRC /resources directory- the classpath: / config /// Project SRC /resources directory- the classpath: /Copy the code
Spring Boot scans all four locations in the figure above, and the higher-priority configuration overrides the lower-priority configuration. If the high-priority configuration file has attributes that do not conflict with the low-priority configuration file, a complementary configuration is formed, that is, all non-conflicting attributes are integrated.
Multiple profiles in SpringBoot can be managed according to the appropriate project mode. It is recommended to use multiple profiles instead of configuring multiple profiles in the same file. When SpringBoot starts, the main Profile is loaded in the specified order. And read the active Profile Profile in the master Profile.