Spring Boot Is it possible to bind an implementation of interface to @ConfigurationProperties

Viewed 250

Good day everyone!

Is it possible to have an interface as configuration property of Spring Boot application, so that Spring Boot could choose implementation based on object properties?

For example, I have a next externalized configuration described in yaml file:

props:
    elements:
        - name: element1
          uniqueProperty1: value1
        - name: element2
          uniqueProperty2: value2

And next class annotated with @ConfigurationProperties and list of elements, implementing Element interface, as property with some implementation classes (code in 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 = ""
}

When I run my application, next error occurs: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'props.elements' to java.util.List<Element>

0 Answers
Related