hi there. I have pretty strange question. I think ...
# spring
m
hi there. I have pretty strange question. I think not sure even that it`s possible to solve. I wan't move kafka configuration to the external module and make it reusable with generic(different Pojo in different modules) I'm not sure what subject should I learn about Spring and Kotlin (reflection probably)
Copy code
@EnableKafka
@EnableAsync
@Configuration
class KafkaConfig<T>(
        @Autowired val objectMapper: ObjectMapper,
        @Value("\${kafka.bootstrap-servers}") val bootstrapServers: String,
        @Value("\${kafka.consumer.group-id}") val groupId: String,
        @Value("\${kafka.consumer.auto-offset-reset}") val autoOffsetReset: String
) {
    @Bean
    fun consumerFactory(): ConsumerFactory<String, T> = DefaultKafkaConsumerFactory(
            consumerConfigs(),
            JsonDeserializer(String::class.java, objectMapper),
            JsonDeserializer(T::class.java, objectMapper)
    )

}
So issue is - T as non a expression can`t provide ::class.java value Can any one help with documentation topic what I should start from?
e
you could try to add
companion object
factory method to help create instance of KafkaConfig with right class field
like this:
Copy code
class KafkaConfig<T>(private val clazz: Class<T>) {
    fun testPrint() {
        println(clazz)
    }

    companion object {
        inline operator fun <reified R> invoke(): KafkaConfig<R> {
            return KafkaConfig(clazz = R::class.java)
        }
    }
}

fun main(args: Array<String>) {
    println(KafkaConfig<String>().testPrint())
}
l
unfortunately this sample is not applicable to spring configuration
@mp use reflection in constructor to obtain actual type parameter and use it