How does spring boot configure configuration files for different environments using profile

In the development of springboot, sometimes we have different configurations, such as log printing, database connection, etc. the configurations of development, testing and production environments may be inconsistent. Fortunately, springboot supports the configuration of different environments through different profiles, The following is a general introduction to YML configuration file and properties configuration file. How to use profile to configure configuration files in different environments

Let's first introduce the development environment:

Let's introduce how to use a YML file to configure property files through different profiles:

1. First, we create an application The properties file of YML is as follows:

2. Then open the file and enter the following contents:

Very simple configuration, application The YML file is divided into four parts, using a set of (- --) as separators. The first part is the general configuration part, which represents the properties common to the three environments, spring profiles. Active will explain later

The last three paragraphs are, production, and all use spring Profiles specifies a value (DEV for development, test for test and Pro for production). This value indicates which profile the configuration should be used in,

PS: be careful!! Be careful!! Be careful!! The value followed by all attributes should be separated by a space with ":", otherwise, the configuration is invalid!!! Invalid configuration!!! Invalid configuration!!! I fell in at the beginning. I tell you 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 in the first paragraph profiles. active=XXX;

XXX above is the spring of each environment The value corresponding to profiles. Through this, you can control which environment configuration file to call for local startup, 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 so will production,

PS: if spring profiles. If active does not specify a value, only spring will be used The value of the profiles file, that is, only the general configuration will be loaded

If it is deployed to the server, we normally make it into a jar package. When it is time to publish, we adopt:

--spring. profiles. Active = test or pro to control which environment configuration is loaded. The complete command is as follows:

java -jar xxxxx. jar --spring. profiles. Active = test means loading the configuration of the test environment

java -jar xxxxx. jar --spring. profiles. Active = Pro indicates that the configuration of the production environment is loaded

After the above introduction, let's start it locally. First, spring profiles. Set the value of active to dev and see the log print results:

Through log analysis, you can see that the value of printed profiles is dev. let's look at the printed port number

It is found that the port number is 8080, indicating that the configuration of dev is loaded;

Let's try to change the value of active to test and restart:

Active becomes test,

The port changes to 8081, indicating that the test configuration is loaded,

Next, use multiple YML configuration files to configure property files:

If multiple ymls are used to configure attributes, we can create application - {profile} through the same explicit specification as the configuration file YML file, which places environment independent attributes in application In the YML file, you can configure the attribute files of multiple environments in this form. In application Specify spring. In the YML file profiles. The value of active = profiles to load the configuration of different environments. If it is not specified, only application. Is used by default The YML property file will not load the configuration of other profiles

Configuring multiple environment profiles using properties

If using application Properties to configure multiple environments. The principle is the same as using multiple YML configuration files. It is also through application - {profile} Properties to control which environment configuration is loaded and placed in application In the properties file, through spring profiles. Active = the value of profiles. If the configuration of different environments is loaded, the application is loaded by default The configuration with profile will not be loaded

summary

The above is what Xiaobian introduced to you about how spring boot uses profile to configure configuration files in different environments. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>