How does Spring Boot use profile to configure configuration files in different environments? springprofile
In springboot development, sometimes we have different configurations, such as log printing, database connection, development, testing, and production. The configuration may be inconsistent in each environment, springboot supports configuring different environments through different profiles. The following describes how to configure different environments using profiles in the yml configuration file and properties configuration file...
First introduce the development environment:
- The jdk version is 1.8.
- Springboot version 1.4.1
- Intellij idea
We will first introduce how to use a yml file to configure attribute files through different profiles:
1. First, create an attribute file named application. yml, as shown below:
2. open the file and enter the following content:
# Check which configuration debug: falsespring: profiles: active: dev --- # Development Environment configuration spring: profiles: devserver: port: 8080 --- # test environment configuration spring: profiles: testserver: port: 8081 --- # production environment configuration spring: profiles: proserver: port: 8082
Very simple configuration, application. the yml file is divided into four parts. A group (---) is used as the separator. The first part is the general configuration part, indicating the attributes common to all three environments. profiles. active will be explained later.
The three sections below are respectively development, testing, and production, all using spring. profiles specifies a value (dev for development, test for production, and pro for production). This value indicates which profile the configuration should be used in,
PS: Be sure to pay attention to it !! Be sure to pay attention to it !! Be sure to pay attention to it !! The values following all attributes must be separated with ":". Otherwise, the configuration is invalid !!! Invalid configuration !!! Invalid configuration !!! I already fell in. Let me tell you, don't fall in like me !!!
If we start it locally, in the general configuration, we can set the profile of the environment to be called, that is, spring. profiles. active = XXX;
The above XXX is the value corresponding to spring. profiles in each environment. Through this, you can control which environment configuration file is called by local startup, for example:
Spring:
Profiles:
Active: dev
The property loaded is the property of the development environment. If dev is changed to test, the property of the test environment is loaded, and the same is true for production,
PS: If no value is specified for spring. profiles. active, only the value of the spring. profiles file is used, that is, the general configuration is loaded.
If it is deployed to the server, we can pack it into a jar package. During the release, we use:
-- Spring. profiles. active = test or pro to control the configuration of the loaded environment. The complete command is as follows:
Java-jar xxxxx. jar -- spring. profiles. active = test indicates loading the configuration of the test environment.
Java-jar xxxxx. jar -- spring. profiles. active = pro indicates loading production environment Configuration
After the above introduction, let's start it locally. Set the value of spring. profiles. active to dev to see the log printing result:
Log analysis shows that the value of profiles is dev. Let's look at the printed port number.
The port number is 8080, indicating that the dev configuration is loaded;
Next we will try to change the value of active to test and restart it:
Active becomes test,
The port is changed to 8081, indicating that the test configuration is loaded,
Next, we will use multiple yml configuration files for Configuration Attribute files:
If you use multiple yml to configure attributes, you can use the same explicit specification as the configuration file to create application-{profile }. the yml file will be placed on environment-independent properties in the application. in the yml file, you can configure attribute files for multiple environments in this form. specify spring in the yml file. profiles. the value of active = profiles to load configurations in different environments. If not specified, only application is used by default. the yml property file does not load the configuration of other profiles.
Use properties to configure multiple environment configuration files
If you use application. properties is used to configure multiple environments. The principle is the same as that of using multiple yml configuration files. application-{profile} is also used }. properties to control the configuration of the loaded environment. It will place environment-independent properties in the application. in the properties file, use spring. profiles. the value of active = profiles. The configuration of different environments is loaded. If not specified, the application is loaded by default. properties configuration, does not load configuration with profile
Summary
The above section describes how Spring Boot uses profile to configure configuration files for different environments. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!