Spring Boot属性取决于特定于配置文件的属性

时间:2020-09-02 14:39:47

标签: java spring spring-boot application.properties spring-boot-configuration

我有一个Spring Boot应用程序,该应用程序应连接到dev和prod中的不同服务器,并在这些服务器上运行许多服务。至此,我已经创建了这样的配置:

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

但是,当我使用@Value("${server.url.srv1}")初始化bean参数时,得到的字符串为“ $ {server.url.base} / srv1”,而不是“ http://192.168.86.17/srv1”或“ https ://10.11.12.3/srv1”。

这完全可行吗?应该只有在所有配置文件都加载后才能解析“ $ {}”引用,但这似乎并非如此。

我在Spring site,谷歌(这使我指出了另外有用的Baeldung site)和此处都找到了答案,但是没有找到与我的特定问题相关的答案。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

运行应用程序时,必须设置要设置的特定spring-profile才能获得先前定义的特定属性。

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

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