【问题标题】:spring boot always using the same profile弹簧靴总是使用相同的配置文件
【发布时间】:2017-09-03 21:18:38
【问题描述】:

我正在使用spring boot 1.5.2,并且使用配置文件但我发现了一个非常奇怪的事情。

我的 Spring Boot 资源文件夹如下:

application.yml 中的配置

spring:
  profiles:
    active: @profileActive@

application-dev.yml

spring:
  profiles: dev
    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db1
      username: root
      password:
server:
  port: 8080

application-test.yml

spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

我的 pom.xml,只包含资源部分和配置文件部分。

<!-- profile -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <profileActive>dev</profileActive>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <build.profile.id>test</build.profile.id>
            <profileActive>test</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
            <profileActive>prod</profileActive>
        </properties>
    </profile>
</profiles>


<resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application-${profileActive}.yml</include>
                <include>application.yml</include>
                <include>templates/*</include>
            </includes>
        </resource>

    </resources>

我现在正在尝试使用测试配置文件,发现一切正常,@profileActive@ 已被替换为test

mvn clean package -Dmaven.test.skip=true -Ptest

看起来一切正常。

但是当我尝试运行 jar 时,它总是使用 dev 配置文件,尽管application.yml 显示我们现在使用test or prod 配置文件。

我不知道我的 yml 配置哪里出了问题。我尝试将所有配置文件配置包含在一个 application.yml 文件中。但应用程序仍在使用dev 配置文件。

在一个 application.yml 文件中完全配置

spring:
  profiles:
    active: @profileActive@

---
spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db1
  username: root
  password:

server:
  port: 8080

---
spring:
  profiles: test
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password:

server:
  port: 8081

---
spring:
  profiles: prod

server:
  port: 9000

最后,我尝试使用 properties 文件,我的所有配置工作正常,当我运行我的应用程序时可以使用正确的配置文件。

现在,我只想知道我的 yml 配置出了什么问题。

提前致谢!

【问题讨论】:

  • 您能否将所有部分放在一个 Git 存储库中,以便轻松查看确切您是如何配置的?
  • 我刚刚创建了一个与您定义的完全相同的简单项目。一切都按预期工作。您可以尝试在 IntelliJ 之外运行 jar 吗?

标签: maven spring-boot maven-profiles spring-profiles spring-properties


【解决方案1】:

尝试像这样运行你的 jar

java -jar -Dspring.profiles.active=test yourapp.jar

或将@profileActive@ 替换为您需要的配置文件的名称。

【讨论】:

    【解决方案2】:

    问题可能是,当 spring boot 读取您的 application.yml 时,它已经决定回退到默认配置文件。从那以后,我认为,切换个人资料为时已晚。

    您可以尝试在名为“bootstrap.yml”的配置文件中定义您的 spring.profiles.active 属性。这可能对你有用。

    【讨论】:

      【解决方案3】:

      我使用spring-boot-starter-web 复制了您的描述,作为能够启动小型应用程序的附加依赖项。

      一切都按预期进行。如果我运行mvn clean package -Ptesttest 配置文件将被激活。与其他配置文件相同。我将所有配置文件放在一个yml 文件中,注释掉&lt;include&gt;application-${profileActive}.yml&lt;/include&gt;,它也可以工作。

      当我像您一样从父文件系统路径执行 JAR 文件时,一切都按预期工作。当我同时使用另一个配置文件执行另一个 Maven 构建并重新启动应用程序时,将使用新的配置文件。

      如果您将mvn clean packagedev 之外的其他配置文件一起使用,则生成的JAR 中不可能有任何application-dev.yml。如果没有 clean 和使用不同 Maven 配置文件的不同构建,它们都将在 JAR 中。

      您使用哪种操作系统?是否有可能在您的 JAR 文件中保存了文件句柄并且不会被替换?但这应该在您的 Maven 构建中发出信号。

      【讨论】:

        【解决方案4】:

        我个人会做以下事情:

        1) 从你的 application.yaml 中删除它(因为你可以为此设置一个 JVM 属性)

        spring:
          profiles:
            active: @profileActive@
        

        2) 从每个配置文件特定的 yaml 文件中删除配置文件特定的标头。您不需要它,因为配置文件基于文件后缀,例如删除这个:

        spring:
          profiles: dev
        

        3) 正如 Oleg 所说,只需使用 Dspring.profiles.active=Whatever 运行您的任务

        【讨论】:

          【解决方案5】:

          终于找到原因了。

          我的项目是一个多模块的maven项目。

          我正在使用yml格式的一个模块,

          其他是property 格式。

          当我将这两个模块都设为yml格式时,一切正常。

          谢谢大家!谢谢!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-12-22
            • 2018-02-14
            • 2017-12-21
            • 2019-05-03
            • 2021-04-20
            • 1970-01-01
            • 2023-01-07
            相关资源
            最近更新 更多