java - 如何从 Spring Boot 配置服务器提供的其他属性文件中的 application.properties 获取 key ?

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

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/

相关文章:

java - 微服务实例间JPA同步

java - Spring-Boot Thymeleaf 以 html 形式加载图像,但列表不显示

unit-testing - 使用 Thymeleaf 模板引擎对服务进行单元测试时出现问题

java - Spring Boot 连接到 mysql docker

java - 查找出现最小值的数组索引

java - sqljdbc4.jar - 无法连接到 MSSQL 服务器实例 - SSL 错误?

java - 特定列的 Spring Data UpdateTimestamp

java - 使用 Spring 缓存抽象的异步缓存更新

java - 无法使用 REST 架构和 Angular JS 在 Spring Boot 中映射新 url,并且发布请求未接收对象字段值

javascript - 在 JavaScript 中访问请求参数