In the development of springboot, sometimes we have different configurations, such as log printing, database connection, etc., development, testing, and production, each environment may be configured inconsistently. Fortunately, springboot supports configuring configurations of different environments through different profiles. Here is a brief introduction to how to use profiles to configure configuration files of different environments using profiles...
Let me introduce the development environment first:
Let’s first introduce the use of a yml file and configure the property file through different profiles:
1. First, we create a property file called application.yml, as follows:
2. Then open the file and enter the following content:
# Check which configurations are enabled for springboot 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: propserver: port: 8082
A very simple configuration, the application.yml file is divided into four parts, using a set of (---) as the separator. The first part is the general configuration part, indicating the properties that are common to all three environments. Spring.profiles.active is explained later.
The following three paragraphs are: development, testing, and production, and use spring.profiles to specify a value (dev, test, and production). This value indicates which profile the configuration of this segment should be used in.
PS: Be sure to pay attention!! Be sure to pay attention!! All attributes followed by value should be separated by ":" by a space, otherwise the configuration will be invalid!!! The configuration will be invalid!!! The configuration will be invalid!!! I fell in at the beginning, so I will tell you here not to fall in like me!!!
If we start locally, in the general configuration, we can set the profile of which environment to call, that is, spring.profiles.active=XXX in the first section;
The above XXX is the value corresponding to spring.profiles of each environment. Through this, you can control which environment configuration file to call locally, for example:
spring:
Profiles:
active: dev
What is loaded is the properties of the development environment. If dev is replaced with test, the properties of the test environment will be loaded, and the same is true for production.
PS: If spring.profiles.active does not specify a value, then the value without specified spring.profiles file will only be used, that is, only the general configuration will be loaded.
If it is deployed to the server, we will normally type into a jar package, and it is time to publish it, and use:
--spring.profiles.active=test or pro controls the configuration of which environment to load. The complete command is as follows:
java -jar xxxxx.jar --spring.profiles.active=test represents the configuration of loading the test environment
java -jar xxxxx.jar --spring.profiles.active=pro represents the configuration of loading production environment
After the above introduction, let's start locally. First set the value of spring.profiles.active to dev and see the log printing results:
Through log analysis, we can see that the value of profiles is dev. Let’s take a look at the printed port number below.
It was found that the port number is 8080, which means that the dev configuration is loaded;
Let's try changing the active value to test and restart it:
Active becomes test,
The port becomes 8081, which means that the configuration of test is loaded.
Next, use multiple yml configuration files to configure the properties file:
If you use multiple yml to configure properties, we can use this way. Through the same clear specification as the configuration file, create an application-{profile}.yml file, and place the environment-independent properties into the application.yml file. You can configure the property files of multiple environments in this form, specify the value of spring.profiles.active=profiles in the application.yml file to load the configuration of different environments. If you do not specify, you will only use the application.yml attribute file by default and will not load the configuration of other profiles.
Configure multiple environment configuration files using properties
If you use application.properties to configure multiple environments, the principle is the same as using multiple yml configuration files. It also uses application-{profile}.properties to control which environment configuration is loaded. It will be placed in the application.properties file for environment-independent properties, and load the configuration of different environments through the value of spring.profiles.active=profiles. If it is not specified, the configuration of application.properties is loaded by default, and the configuration with profile will not be loaded with the configuration with the profile.
Summarize
The above is the configuration files for Spring Boot using profiles to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!