Interface implementations that use nested type parameters with Kotlin types are not found as implementations. The following example fails:
package com.example.demo
import org.springframework.stereotype.Component
interface MyInterface<T> {
fun doit(): T
}
@Component
class MyImplementation1 : MyInterface<Sequence<Int>> {
override fun doit(): Sequence<Int> {
TODO("not implemented")
}
}
@Component
class MyConsumer(private val my: MyInterface<Sequence<Int>>)
The error message is:
Parameter 0 of constructor in com.example.demo.MyConsumer required a bean of type 'com.example.demo.MyInterface' that could not be found.
But if I use the (similar) Java interface Stream
everything is fine:
package com.example.demo
import org.springframework.stereotype.Component
import java.util.stream.Stream
interface MyInterface<T> {
fun doit(): T
}
@Component
class MyImplementation1 : MyInterface<Stream<Int>> {
override fun doit(): Stream<Int> {
TODO("not implemented")
}
}
@Component
class MyConsumer(private val my: MyInterface<Stream<Int>>)
The application file is identical in both cases:
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
Possibly related to #22313
Comment From: encircled
Hi,
Yes, this is the same issue as #22313, Kotlin Sequence
class has out
variance, which causes the problem.
Comment From: hpy1994-java
oops~
Comment From: sdeleuze
I close this issue since that's a duplicate of #22313 indeed.