Spring Boot依赖于特定配置文件的属性

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

Spring Boot properties that depend on profile-specific properties

问题

application.properties:

server.url.srv1=${server.url.base}/srv1
server.url.srv2=${server.url.base}/srv2
server.url.srv3=${server.url.base}/srv3

application-dev.properties:

server.url.base=http://192.168.86.17

application-prod.properties:

server.url.base=https://10.11.12.3

Yet when I initialize a bean argument with @Value("${server.url.srv1}"), I get a string of "${server.url.base}/srv1" and not "http://192.168.86.17/srv1" or "https://10.11.12.3/srv1" as expected.

Is this doable at all? It should be if the "${}" references are only resolved once all the config files are loaded, but this doesn't seem to be the case.

英文:

I have a Spring Boot application which should connect to different servers in dev and prod, with many services running on those servers. To this point, I have created the configuration like this:

application.properties:

server.url.srv1=${server.url.base}/srv1
server.url.srv2=${server.url.base}/srv2
server.url.srv3=${server.url.base}/srv3

application-dev.properties:

server.url.base=http://192.168.86.17

application-prod.properties:

server.url.base=https://10.11.12.3

Yet when I initialize a bean argument with @Value("${server.url.srv1}"), I get a string of "${server.url.base}/srv1" and not "http://192.168.86.17/srv1" or "https://10.11.12.3/srv1" as expected.

Is this doable at all? It should be if the "${}" references are only resolved once all the config files are loaded, but this doesn't seem to be the case.

I have searched for an answer on both the Spring site, on Google (which pointed me to an otherwise useful Baeldung site), and here, but found nothing relevant to my particular question.

答案1

得分: 1

application.properties中的占位符应该可以工作。请参考我已经添加了您的用例并且按预期工作的示例项目:https://github.com/itsprav/spring-profile-properties-using-placeholder

英文:

Placeholders in the application.properties should work. Please refer sample project I have added with your use case and it work as expected: https://github.com/itsprav/spring-profile-properties-using-placeholder

答案2

得分: 0

当您运行应用程序时,必须设置特定的 Spring 配置文件以获取先前定义的特定属性。

有许多方法可以设置这些配置文件。

以不同方式设置配置文件(JVM、编程方式、环境变量...)

英文:

When you run your application you must have to set the specific spring-profile to be set in order to get the specific properties defined previously.

There is many ways to set these profiles.

Setting Profiles in different ways (JVM, Programmatically, Environment Variable...)

huangapple
  • 本文由 发表于 2020年9月2日 22:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707926.html
匿名

发表评论

匿名网友

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

确定