如何从Spring Boot Config Server提供的其他属性文件中获取application.properties中的密钥?

时间:2017-12-29 06:40:07

标签: java spring spring-boot spring-boot-configuration

Spring集中配置的官方教程(https://spring.io/guides/gs/centralized-configuration/)说:

  

它还将发送来自任何名为的文件的所有值   git存储库中的application.properties或application.yml。

我想在特定属性文件a-bootiful-client.properties中使用该文件中的一些属性。

有可能吗?我尝试过,但占位符对我不起作用。

例如,我在application.properties文件中有一个键值对key1=val1。然后在a-bootiful-client.properties文件中,我尝试将该密钥作为another.key=${key1}-extraVal访问。

由于

1 个答案:

答案 0 :(得分:1)

如果您在Spring项目中使用bootstrap.properties文件并将其放在application.properties(src / main / resources)旁边,那么这是可能的。在应用程序的引导期间加载此属性字段,您可以执行以下操作:

# content of your bootstrap.properties
spring.application.name=a-bootiful-client
spring.cloud.config.uri=YOUR-CONFIG-SERVER-URI
key1=value1

将以下内容添加到a-bootiful-client.properties文件中:

# content of your a-bootiful-client.properties file in your Git repo

another.key=${key1}-extraVal

现在,您可以访问Spring应用程序代码中的another.key值,如:

@Value("${another.key}")
private String myOtherKey;

并且值将合并。