SoFunction
Updated on 2025-04-05

The configuration reading method of springboot project different environments

1. The first thing I know is

The springboot project can use the following three types of configuration files and can exist at the same time.

The different configuration contents read by springboot will be merged. If there are configurations with the same configuration items, they will take effect in the following priority order:

> >

2. The same type of configuration file

Placed in different locations, the order of effectiveness is as follows:

  • Level 1: Directory of the same level: config/[highest)
  • Level 2: Directory of the same level:
  • Level 3: classpath: config/
  • Level 4: classpath:

3. In a configuration file

You can also configure different environment configurations as follows

#Set the enabled environmentspring:
  profiles:
    active: dev

---
spring:
  profiles: dev
server:
  port: 81


---
spring:
  profiles: test
server:
  port: 82


---
spring:
  profiles: pro
server:
  port: 83

4. You can read the configuration in the pom file in the file

Can be used to configure the current environment

#Set the enabled environmentspring:
  profiles:
    active: ${}

---
spring:
  profiles: dev
server:
  port: 81


---
spring:
  profiles: test
server:
  port: 82


---
spring:
  profiles: pro
server:
  port: 83

The configuration in the pom file is as follows:

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
&lt;profiles&gt;
        &lt;!--devenvironment--&gt;
        &lt;profile&gt;
            &lt;id&gt;dev&lt;/id&gt;
            &lt;properties&gt;
                &lt;&gt;dev&lt;/&gt;
            &lt;/properties&gt;
        &lt;/profile&gt;

        &lt;!--testenvironment--&gt;
        &lt;profile&gt;
            &lt;id&gt;test&lt;/id&gt;
            &lt;properties&gt;
                &lt;&gt;test&lt;/&gt;
            &lt;/properties&gt;

        &lt;/profile&gt;

        &lt;!--proenvironment--&gt;
        &lt;profile&gt;
            &lt;id&gt;pro&lt;/id&gt;
            &lt;properties&gt;
                &lt;&gt;pro&lt;/&gt;
            &lt;/properties&gt;
            &lt;activation&gt;
                &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
            &lt;/activation&gt;
        &lt;/profile&gt;
    &lt;/profiles&gt;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.