Hi everyone,
I’m working on generic class with bean-s definition inside. unfortunately the last bean I need to autowire is with the same name and in second specialization of this generic I have a problem with autowiring in One class…
In shortcut I have generic like this:
class Generic<T> {
@Bean
open fun someFun():T {
return someProxyWhichReturnT()
}
}
Now I define 2 classes
open class FromGeneric1 :
Generic<MyType1>
open class FromGeneric2 :
Generic<MyType2>
And in my class I want to use them as:
class TestClass {
@Autowired
private lateinit var someFun: MyType1
@Autowired
private lateinit var someFun: MyType2
}
Unfortunately it is not possible because of duplicate in names
So I try qualifiers like this:
class TestClass {
@Autowired
@Qualifier("someFun")
private lateinit var someFun1: MyType1
@Autowired
@Qualifier("someFun")
private lateinit var someFun2: MyType2
}
Not working also, because of undistinguished classes ( one generic )
Solution should be to use @Qualifier inside generic and specialization this at compile time or make function name varying dynamicly by using tempalte or some companion object, I mean
1)
open class Generic<T> {
@Bean
@Qualifier(name="${T::toString}")
open fun someFun():T {
return someProxyWhichReturnT()
}
}
OR
open class Generic<T,V> {
@Bean
@Qualifier(name="${V::toString}")
open fun someFun():T {
return someProxyWhichReturnT()
}
}
- dynamic function name
class Generic<T> { @Bean open fun someFun_${T::toString} ():T { return someProxyWhichReturnT() } }
For now I see no options, but maybe in the future this generics will be more resemble C++ templates and It will be feasible.
Regards,
Damian