java - Spring Boot是否可以将接口的实现绑定到@ConfigurationProperties
问题描述
今天是个好日子!
是否可以将接口作为Spring Boot应用程序的配置属性,以便 Spring Boot 可以根据对象属性选择实现?
例如,我有一个在yaml文件中描述的下一个外部化配置:
props:
elements:
- name: element1
uniqueProperty1: value1
- name: element2
uniqueProperty2: value2
@ConfigurationProperties
下一个用元素列表注释的类,实现Element
接口,作为具有一些实现类的属性(Kotlin中的代码):
@ConfigurationProperties(value = "props")
class ConfigProperties() {
var elements: List<Element> = emptyList()
}
interface Element {
val name: String
}
class ElementImpl1 : Element {
override val name: String = ""
val uniqueProperty1: String = ""
}
class ElementImpl2 : Element {
override val name: String = ""
val uniqueProperty2: String = ""
}
当我运行我的应用程序时,会发生下一个错误:
org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'props.elements' to java.util.List<Element>
解决方案
推荐阅读
- r - 'newdata' 有 1 行,但找到的变量有 10 行
- python - 如何将大 JSON 文件导入 MYSQL
- c# - 在 .NET 中将 Opus (.ogg) 转换为 PCM (.wav)
- excel - 将多个工作簿合并为一个
- augmented-reality - 为什么 ARKit 需要 iOS 11 和 A9 而其他 sdks 像 Vuforia 或 Wikitude 可以做同样的事情?使用较低的 iOS 版本
- c# - 不使用 RegEx 和/或列表/规则的密码检查
- javascript - Amazon S3:getSignedUrl:“参数中缺少必需的密钥‘Bucket’”
- windows - 批处理文件,检查是否返回错误然后做些什么?
- sql - 如何避免在无键表上插入重复项?
- mysql - InnoDB 在外键上使用 where 子句时锁定不同的索引