java - 在嵌套地图中重用弹簧配置属性
问题描述
我有一个支持多租户的 Spring Boot 应用程序。我希望支持一组自定义配置属性的全局和每租户配置。对于每个属性,可以配置一个全局值,但对于特定租户,该值可能会被覆盖。
目前我只有全局属性,使用一些用@ConfigurationProperties
. 例如
@ConfigurationProperties("props1")
class Props1 {
...
}
@ConfigurationProperties("props2")
class Props2 {
...
}
@ConfigurationProperties("props3")
class Props3 {
...
}
我希望能够像这样配置属性:
props1.a=...
props1.b=...
props2.x=...
props2.y=...
props3.z=...
tenant.some-tenant-id.props1.b=...
tenant.some-tenant-id.props3.z=...
tenant.some-other-tenant-id.props2.x=...
我正在考虑添加另一个@ConfigurationProperties
类,例如前缀tenant
和每个现有类 a Map
,其中键将是租户名称,例如:
@ConfigurationProperties("tenant")
class TenantSpecificProps {
private Map<String, Props1> props1;
private Map<String, Props2> props2;
private Map<String, Props3> props3;
...
}
但是我担心重用@ConfigurationProperties
为地图注解的类可能会在以后导致问题,因为这不是在 Spring 中使用这些类的正常方式。此外,我需要手动合并全局值和每个租户值(每个租户覆盖全局值)。
这是一种有效的方法,和/或有更惯用的方法吗?
解决方案
推荐阅读
- objective-c - 从给定键值对的字典数组中过滤 NSDictionary
- java - sun.* Java源码在哪里下载?
- java - 为什么我的代码有 IllegalStateException 错误
- sql - Excel VBA 为 Access 生成的 INSERT 语句在 VBA 中不起作用,但在 Access 中起作用
- database - 优雅地关闭 Elasticsearch 索引
- python - Python Actor 模型 + 异步(?)
- javascript - 无法一一访问以下结构?
- python-sphinx - 是否可以在 Sphinx 中保留长线?
- python - 在嵌套列表中查找匹配元素
- apache-kafka - Kafka Streaming 应用程序在与 Kafka 连接后仅读取最新消息