【问题标题】:Custom application properties in spring boot using IntelliJ使用 IntelliJ 在 Spring Boot 中自定义应用程序属性
【发布时间】:2020-04-30 05:00:37
【问题描述】:

我正在尝试使用 application.properties 以外的应用程序属性,例如资源目录中的 application_local.properties

这样我就可以拥有 2 个属性文件,一个用于本地,另一个用于服务器。

正如许多博客中提到的,我应该使用以下命令:

spring-boot:run -Dspring.config.location=/Users/myuser/work/MyProject/my-app/src/main/resources/application_local.properties

但这不起作用,它仍在从 application.properties 获取值。

我错过了什么,请提出建议?

谢谢

【问题讨论】:

标签: java spring-boot intellij-idea


【解决方案1】:

1- 遵循命名约定 application-{profile}.properties

  • application-local.properties

2 套配置文件

  • -Dspring.profiles.active=local

简单来说,你可以使用这两个链接:

  1. How to load property file based on spring profiles
  2. spring-profiles

【讨论】:

    【解决方案2】:

    -Dspring.profile.location 将目录作为输入。此属性的目的是指定额外的目录位置来保存您的属性文件。

    您在命令中使用了属性文件名。

    详细参考@Answer at other thread here

    您可以按照@mehardad的建议使用

    【讨论】:

      【解决方案3】:

      -D 选项将参数发送到 Java 虚拟机。为了向Spring boot发送参数,必须使用'--'命令选项。

      例子:

      假设,在 application.properties 文件中定义了一个名为“spring.profiles.active”的选项,如下所示:

      spring.profiles.active=dev

      可以使用命令行参数覆盖该选项,如下所示:

      java -jar application.jar --spring.profiles.active=prod
      

      【讨论】:

        【解决方案4】:

        使用 Spring 配置文件并在运行时选择,本地会

        -Dspring.profiles.active=local
        

        属性文件应该叫application-local.properties

        【讨论】:

          猜你喜欢
          • 2015-03-25
          • 1970-01-01
          • 2020-10-26
          • 2018-08-03
          • 1970-01-01
          • 2015-11-21
          • 2021-10-08
          • 2015-05-21
          相关资源
          最近更新 更多