【问题标题】:Springboot application not using the correct .properties fileSpring Boot 应用程序未使用正确的 .properties 文件
【发布时间】:2021-11-15 22:25:08
【问题描述】:

我有一个简单的 Springboot 应用程序,可以使用以下命令运行:./mvnw spring-boot:run。如果我将我的数据库的 URI 放在 application.properties 文件中,这可以正常工作,但问题是这个文件由 Heroku 使用,并不适合我的本地使用。

所以我遇到了一个 Stackoverflow 答案,说我可以简单地创建另一个 .properties 文件,但将其命名为 application-dev.properties,然后当我运行我的应用程序时,当我设置活动时,将自动选择正确的 .properties 文件个人资料到dev

所以我尝试了以下操作:

  1. 使 application.properties 文件使用 Heroku 的环境变量,因为这是我不想在本地使用的 .properties 文件。
  2. 我创建了一个名为 application-dev.properties.properties 文件,其中包含以下行:
spring.data.mongodb.uri=mongodb+srv://MY_NAME:MY_PASSWORD@springbootcluster.v1maw.mongodb.net/Employees?retryWrites=true&w=majority
  1. 我这样运行应用程序:./mvnw spring-boot:run -Dspring.profiles.active=dev
  2. 应用因尝试使用 application.properties 文件而不是 application-dev.properties 文件而失败并出现大量不同的错误

部分错误信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';

ERROR MESSAGES

【问题讨论】:

    标签: java spring-boot environment-variables spring-data


    【解决方案1】:

    -Dspring.profiles.active 在运行 Maven 的 JVM 中设置 spring.profiles.active 系统属性,而不是在运行应用程序的 JVM 中。要解决此问题,请使用spring-boot.run.jvmArguments 系统属性来配置用于运行应用程序的 JVM 的参数:

    ./mwnw -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
    

    另外,还有一个专门用于设置活动配置文件的属性,它稍微简洁一些:

    ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
    

    您可以在relevant section of the reference documentation for Spring Boot's Maven plugin 中了解更多信息。

    【讨论】:

      猜你喜欢
      • 2016-03-08
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2018-12-30
      • 2017-08-25
      • 2020-03-30
      相关资源
      最近更新 更多