Home > Java > javaTutorial > body text

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

王林
Release: 2023-05-12 23:58:16
forward
1776 people have browsed it

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
Copy after login

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

spring.profiles.active=@spring.profiles.active@
Copy after login

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>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template