[Java] There are multiple configuration files under the resource directory in the SpringBoot project. How to specify a specific configuration file to take effect?

When we develop a project, there will be multiple development environments, such as test environment, production environment, development environment, product environment and other environments, as follows How
insert image description here
insert image description here
many configuration files, so which configuration file will take effect? How do we specify which configuration files we need? How to specify the configuration file that takes effect by default?
I found the following line when the program was running.
insert image description here
That is, if there are so many configuration environments by default, the dev configuration will be enabled by default. Can we specify our configuration file more explicitly through configuration? The answer is yes: several methods are introduced as follows
1: First, use the global configuration directly
You can create a config package in the project, and then use the config package to specify the current development environment
2: You can also use the System class to specify the configuration file, Of course, we definitely do not recommend this method
insert image description here
3: You can also configure the startup parameters of the JVM

java -jar -Dspring.profiles.active=dev demo-0.0.1-SNAPSHOT.jar

4: This is the method I use, as follows

   <build>
        <defaultGoal>compile</defaultGoal>
        <finalName>easy_config</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>environment/</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/environment/${running.env}</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.8</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <running.env>dev</running.env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <running.env>test</running.env>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <running.env>qa</running.env>
            </properties>
        </profile>
        <profile>
            <id>stage</id>
            <properties>
                <running.env>stage</running.env>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <running.env>product</running.env>
            </properties>
        </profile>
    </profiles>

If you see the following line of code, you will find the effect. We can use the activation tag to control which file in the resource file to use.

		<profile>
            <id>dev</id>
            <properties>
                <running.env>dev</running.env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

5: Use IDEA configuration
insert image description here
The effect of using this configuration is that you have files of type application-{spring.profiles.active}.yml, and they should exist directly and explicitly
insert image description here

Guess you like

Origin blog.csdn.net/Zhangsama1/article/details/131469731