https://kotlinlang.org logo
#spring
Title
m

Martin Schwitalla

02/08/2018, 6:39 PM
I have a question. At work we are currently experimenting with Kotlin and Spring Boot. One of the nice things of Java and Spring Boot was, that if you had something like "public interface EventProcessor<T>" in Java you could autowire a list of classes that implement that interface. While trying to do this today with Kotlin it didn't work. Why is that so? And is there an alternative to do a similar thing "the kotlin way"? Currently we just transform the object and based on the type we execute a different processor. This is done via a "when (x)..." but that isn't as nice.
n

nfrankel

02/08/2018, 6:45 PM
could you share the code? because that works in exactly the same way
m

Martin Schwitalla

02/08/2018, 6:48 PM
will do, give me one second
this is the interface
Copy code
interface EventProcessor<T> {
    
    fun process()
}
how can I properly share code? 😄
c

Czar

02/08/2018, 6:53 PM
` ``` `
m

Martin Schwitalla

02/08/2018, 6:53 PM
Copy code
@Service
class SomeEventProcessor() : EventProcessor<LegacyEvent>{
    override fun process() { 
    }

}
c

Czar

02/08/2018, 6:53 PM
when it's large, use insert snippet
m

Martin Schwitalla

02/08/2018, 6:54 PM
Copy code
@Service
class EventProcessorHandler(val processors: List<EventProcessor>)
then this is the code that doesn't work
In java it would have worked
any idea why?
@nfrankel
and thanks @Czar
c

Czar

02/08/2018, 6:59 PM
@Martin Schwitalla Your example works for me after I fix syntax errors. Take a look at a snipped I've posted.
4 Views