spring-guides/tut-spring-boot-kotlin

what's the right way to use kotlin data class to instance different impl of an interface

Storm-Born opened this issue · 2 comments

I have some configurations in my application.properties file. they have diffent prefix to distinguish diffent profiles.

ie:
config.dev.name=xxx
config.dev.age=xxx

config.taas.name=ooo
config.taas.age=ooo

when using java, I can definite an interface first, then definite two implements with annotions @component, @ConfigurationProperties, @Profile.by this way, spring can create diffent implement when activate diffent profile.
I can just definite a member which has the interface type with @autowire in another class. so I can get different values in
different profiles.

but when combining annotions such as @ConstructorBindind @propertysource @Profile with kotlin data class.
it's not work. because I have to put all the implements in the @EnableConfigurationProperties. so spring do not know which one
to choose when use the interface in another calss with @autowire, because spring have created all the implements.

Maybe data classes are not a good fit with this use case and you could leverage concise Kotlin syntax for regular classes that implement interfaces ?

Closing due to lack of feedback.