Home  >  Article  >  Java  >  How does SpringBoot load multiple configuration files to switch between dev and product environments?

How does SpringBoot load multiple configuration files to switch between dev and product environments?

王林
王林forward
2023-05-12 23:58:161760browse

1. Multi-environment switching in SpringBoot

In SpringBoot, in addition to application.properties, the file names of other configuration files we create need to meet application-{profile}.properties format, where {profile} corresponds to your environment identifier (not necessarily a .properties file, it can also be .yml) and its corresponding {profile} value is customized by the developer (such as dev, product), when starting the project, you only need to add the corresponding parameters, and springboot will read the configuration file. The specific profile configuration is set in the application.properties file through the spring.profiles.active property. Next, we use an example to illustrate

(1) First, five configuration files of dev, product, qa, stage and default application are created here

How does SpringBoot load multiple configuration files to switch between dev and product environments?

(2) When loading the configuration file, the application.properties configuration file will be loaded first (some public configurations are generally stored here), and the configuration file of the environment to be loaded is configured in this file. There are two configuration methods.

For example, to load the dev environment, you can configure it like this in application.properties

spring.profiles.active=dev

or use @spring.profiles.active@, as shown below

spring.profiles.active=@spring.profiles.active@

If you use this In this way, you need to add the following content to pom.xml, where the activeByDefault tag specifies the configuration file that is loaded by default when the project starts.

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <spring.profiles.active>dev</spring.profiles.active>
    </properties>
  </profile>
  <profile>
    <id>qa</id>
    <properties>
      <spring.profiles.active>qa</spring.profiles.active>
    </properties>
  </profile>
  <profile>
    <id>stage</id>
    <properties>
      <spring.profiles.active>stage</spring.profiles.active>
    </properties>
  </profile>
  <profile>
    <id>product</id>
    <properties>
      <spring.profiles.active>product</spring.profiles.active>
    </properties>
  </profile>
</profiles>

When the mvn clean package -P dev command is executed to package and publish the project, @spring.profiles.active@ in the configuration file in the jar/war package will be replaced with dev.

Note@spring.profiles.active@ must be consistent with the label <spring.profiles.active></spring.profiles.active>> in the pom, otherwise it will Report an error.

How does SpringBoot load multiple configuration files to switch between dev and product environments?

2. Configuration file loading order in SpringBoot

Priority sorting of configuration files(Which configuration file is the Accurate):

1. The config directory under the project root directory. [Highest priority]
2. Project root directory.
3. The config directory under classpath.
4. Classpath directory (the default location of application.properties when creating a new project). [Lowest priority]

The loading order of configuration files is opposite to the order of priority. The one with lower priority is loaded first, because if there are duplicate configurations, the configuration file loaded first will be overwritten.

In the same level directory, if application.yml and application.properties configuration files exist at the same time, the application.properties configuration file will prevail, that is to say Load the .yml file first and then the .properties file.

How does SpringBoot load multiple configuration files to switch between dev and product environments?

Priority order: 1->2->3->4->5.

File loading order: 5->4->3->2->1.

The above is the detailed content of How does SpringBoot load multiple configuration files to switch between dev and product environments?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete