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:
|