【Java】SpringBoot项目中resource目录下有多个配置文件,如何指定某个特定的配置文件生效?

在我们开发项目的时候,会有多个开发环境,比如测试环境,生产环境,开发环境,产品环境等环境,如下
在这里插入图片描述
在这里插入图片描述
怎么多的配置文件,那么哪一个配置文件会生效?我们如何指定我们需要的配置文件?如何指定默认生效的配置文件?
我在程序运行的时候发现了如下一行
在这里插入图片描述
也就是默认如果说存在这么多的配置环境,默认会开启dev的配置,那么我们是否能通过配置的方式更加显式的指定我们的配置文件呢?答案是可以的:如下介绍几种方法
1:首先是直接使用全局配置
你可以在项目创建一个config包,然后使用config包指定当前的开发环境
2:你也可以使用System类进行配置文件的指定,当然我们肯定不推荐这种方式
在这里插入图片描述
3:你也可以配置JVM的启动参数

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

4:这是我使用的方法,如下

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

如果你看到了如下这一行代码,你就会发现效果,我们可以使用activation这个标签来控制具体使用资源文件中的哪一个文件。

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

5:使用IDEA的配置
在这里插入图片描述
使用这种配置的效果是你存在有application-{spring.profiles.active}.yml类型的文件,并且他们应该直接显式的方式存在
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zhangsama1/article/details/131469731
今日推荐