This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11427|Reply: 1

[Communication] Spring Boot uses profiles to configure profiles for different environments

[Copy link]
Posted on 9/5/2018 1:16:23 PM | | | |
In the development of springboot, sometimes we will have different configurations, such as log printing, database connection, etc., development, testing, production Each environment may not be consistent, fortunately, springboot supports configuring the configuration of different environments through different profiles, the following is a general introduction to how to use the YML configuration file and properties configuration file to configure the configuration file of different environments...

Let's start with the development environment:

The JDK version is 1.8
The version of springboot is 1.4.1
The development tool is IntelliJ IDEA

Let's start by using a YML file to configure the attribute file through different profiles:

1. First, let's create a property file called application.yml

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


Very simple configuration, application.yml file divided into four parts,Use a set (---) as a separator,The first part is the general configuration part, which represents a property that is common to all three environments, spring.profiles.active, which will be explained later.

The next three paragraphs are, development, testing, and production, all of which use spring.profiles to specify a value (development for dev, test for test, production for pro), this value indicates which profile the segment configuration should be used in,

PS:一定要注意!!一定要注意!!一定要注意!!All attributes followed by value should be separated by a space with ":", otherwise, the configuration will be invalid!!Invalid configuration!! Invalid configuration!! I fell in the first place, and I tell you not to fall in like me!!

If we are starting locally, in the general configuration, we can set the profile of which environment to call, that is, spring.profiles.active=XXX in the first paragraph;

You can configure the attribute files of multiple environments in this form, and specify the value of spring.profiles.active=profiles in application.yml file to load the configurations of different environmentsIf not specified, only the application.yml attribute file will be used by default, and the configuration of other profiles will not be loaded



The Spring Boot project gets the application configuration file parameters

import org.springframework.core.env.Environment

The core configuration file application.properties reads as follows:







Previous:In SQL Server, a string is stitched with the value found in SQL
Next:Understanding of the instanceof keyword in Java
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com