问题
Disclaimer: New to Kotlin, may be a easy to solve issue or misunderstood basics.
I am trying to inject a List of a specific interface's Spring implementations, in a regular java class this have been easy like this:
@Autowired
List<IMyClass> myClassList;
But in Kotlin doing the following gives me a error
@Autowired
lateinit private var myClassList: List<IMyClass<Any>>
// No beans of '? extends IMyClass<Object>' or 'List<? extends IMyClass<Object>>' types found
Doing it like this:
@Autowired
lateinit private var myClassList: List<IMyClass<*>>
Makes the injection work but doesn't allow me to use a function defined in the interface that takes a generic object as input
// Out-projected type 'IMyClass<*>' prohibits the use of 'public abstract fun myFunction(obj: T!): T! defined in com.path.IMyClass'
So how am I supposed to solve this? Is it easier to rewrite the interface in Kotlin with some specific syntac?
回答1:
Thing you're doing in Java is just using implicit wildcard. So you have 2 ways here:
- Try to refactor API and inject list of
List<IMyClass<? extends SomeInterface>>
- Use injected
List<IMyClass<*>>
but cast it explicitly to thing you need, i.e.myClassList as List<IMyClass<Any>>
Thing here is kotlin's erasure is more explicit. If you don't know what type is there — we can't guarantee your code will work, because there is such type in kotlin as Nothing
, instance of which can't exist.
回答2:
I faced a similar situation and as answered by asm0dey
, casting was solution which I felt was better for me, but doing it in a cleaner way was a concern.
Just answering below how exactly I did it in the best possible way that I could think of:
@Service
class MyService(_myClassList: List<IMyClass<out Any>>) {
@Suppress("UNCHECKED_CAST")
val myClassList: List<IMyClass<Any>> = _myClassList.map { it as IMyClass<Any> }
// ...
}
来源:https://stackoverflow.com/questions/48320519/spring-inject-list-of-generic-interface-implementations-in-kotlin