Spring 集中配置官方教程 ( https://spring.io/guides/gs/centralized-configuration/ ) 说:
It will also send all the values from any file named application.properties or application.yml in the Git repository.
我想在特定属性文件 a-bootiful-client.properties
中使用该文件中的一些属性。
这可能吗?我尝试过,但占位符对我不起作用。
例如,我在 application.properties 文件中有一个键值对 key1=val1
。然后在 a-bootiful-client.properties
文件中,我尝试以 another.key=${key1}-extraVal
的形式访问该 key 。
谢谢
最佳答案
如果您在 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;
并且这些值将被连接起来。
关于java - 如何从 Spring Boot 配置服务器提供的其他属性文件中的 application.properties 获取 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48018530/