Spring Boot 多环境配置:解决 'No active profile set, falling back to 1 default profile: 'default'' 错误

在使用 Spring Boot 进行多环境配置时,经常会遇到 'No active profile set, falling back to 1 default profile: 'default'' 的错误。这通常是因为你没有在命令行中指定要激活的 profile。

例如,你打包了 'application-prod.properties' 文件到 'target' 目录下,但依然出现了这个错误。这可能是因为你的 pom 文件中的 profiles 配置没有正确配置,或者没有在命令行中指定激活的 profile。

pom 文件中 profiles 配置

你的 pom 文件中的 profiles 配置应该类似以下代码:

<profiles>
    <!--dev环境-->
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!--prod环境-->
    <profile>
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
        </properties>
    </profile>
</profiles>

这段代码定义了两个 profile:'dev' 和 'prod'。'dev' 环境被设置为默认激活,而 'prod' 环境需要手动指定。

命令行指定激活的 profile

为了激活 'prod' 环境,你应该在命令行中使用以下命令进行打包:

mvn clean package -Pprod

这样就可以激活 'prod' 环境的配置文件。

错误原因和解决方法

如果你没有指定激活的 profile,Spring Boot 会默认使用 'default' profile,因此会导致 'No active profile set, falling back to 1 default profile: 'default'' 错误。

为了解决这个问题,你需要在命令行中指定要激活的 profile,例如:

mvn clean package -Pprod

这样就可以激活 'prod' 环境的配置文件,并解决 'No active profile set' 错误。

小结

Spring Boot 多环境配置需要在 pom 文件中定义不同的 profile,并通过命令行指定要激活的 profile。如果你没有指定激活的 profile,Spring Boot 会默认使用 'default' profile,因此会导致 'No active profile set' 错误。

希望本文能够帮助你解决 'No active profile set' 错误。如果你还有其他问题,欢迎在评论区留言。

Spring Boot 多环境配置:解决 No active profile set, falling back to 1 default profile: 'default' 错误

原文地址: https://www.cveoy.top/t/topic/lPVk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录