在 jar 外部加载 application.properties

Ric*_*ano 5 jar properties path spring-boot

如何在SpringBoot中的.jar之外设置application.properties?我将链接到该文件并从代码加载属性。

Ume*_*wal 7

按照惯例,Spring Boot 按照以下优先顺序在 4 个预定位置查找外部化配置文件 \xe2\x80\x93 application.properties 或 application.yml \xe2\x80\x93:

\n
    \n
  1. /config 当前目录的子目录
  2. \n
  3. 当前目录
  4. \n
  5. 类路径/配置包
  6. \n
  7. 类路径根
  8. \n
\n

您可以将 application.properties 放置在 4 个位置中的任何一个位置,而无需在执行 jar 时提供 application.properties 的位置。如果您想指定任何其他自定义位置,则必须在执行 jar 时提供配置位置的路径:

\n
java -jar -Dspring.config.location=<path-to-file> myProject.jar\n
Run Code Online (Sandbox Code Playgroud)\n

来源: https: //www.baeldung.com/spring-properties-file-outside-jar

\n