外部化配置在Spring Boot中

huangapple go评论67阅读模式
英文:

Externalized Configuration in Spring boot

问题

我有一个外部配置文件(位于JAR文件外部)。我尝试运行并期望外部文件中的值将覆盖内部文件(JAR文件中的application.properties\resource\中)中的值。
我阅读了文档并尝试了以下内容:

java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties

但这并未生效。

我的JAR文件位于/target/目录,我的production.properties文件也在其中(在/target/中)。

我该如何解决这个问题?

  • 我应该将外部配置文件放在哪里?

  • 我需要做些什么?

英文:

I have a external configuration file(out side jar). I try to run and expected
that value in external file will override value in internal file(application.properties in \resource\ - in jar file).
I read Documentation and try this:

java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties

This not working.

My jar file at \target\ directory and my production.properties too(at \target\)

How can I resolve my problem?

  • Where should I put external config file ?

  • And what I have to do ?

答案1

得分: 4

从Spring Boot 2.0开始,可以使用属性spring.config.additional-location。使用这个属性,您可以设置外部配置文件,但是来自该配置的属性只会覆盖内部配置中的相应属性,而其他属性保持不变。

有关详细信息,请参见文档

如果您需要完全覆盖整个配置,则继续使用spring.config.location属性。

英文:

Starting from Spring Boot 2.0 it's possible to use property spring.config.additional-location. With this property, you can set external config file, but properties from that config will only override the corresponding ones from internal config, leaving other properties unchanged.

More about it in docs.

If you need to completely override the whole config, then continue to use spring.config.location property instead.

答案2

得分: 0

按照惯例,Spring Boot会按以下优先顺序在4个预定位置中寻找外部配置文件 - application.properties或application.yml:

1)当前目录的/config子目录
2)当前目录
3)类路径下的/config包
4)类路径根目录

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

java -jar -Dspring.config.location=<path-to-file> myProject.jar

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

英文:

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:

  1. /config subdirectory of the current directory
  2. The current directory
  3. Classpath /config package
  4. The classpath root

You can place your application.properties in any of the 4 locations without needing to give the location of application.properties while executing the jar. If you want to given any other custom location , then you will have to provide the path of the config location while executing the jar:

java -jar -Dspring.config.location=&lt;path-to-file&gt; myProject.jar

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

huangapple
  • 本文由 发表于 2020年9月18日 16:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63951767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定