I've a Problem with Spring when using generics. The following code describes the problem pretty good:
public class TestInj<S> {
S elem;
public S getElem() {
return elem;
}
public void setElem(S elem) {
this.elem = elem;
}
}
@Component
public class First extends TestInj<String> {
public First() {
setElem("abc");
}
}
@Component
public class Second extends TestInj<Integer> {
public Second() {
setElem(2);
}
}
public class BaseTest<T> {
@Autowired
protected TestInj<T> test;
}
@Named
public class Test extends BaseTest<String> {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("conf-spring.xml");
context.refresh();
Test test = (Test) context.getBean("test", Test.class);
System.out.println(test.test.getElem());
}
}
The Problem is, that in class BaseTest the class First should be injected, because it has the generic type String. But spring doesn't get that and tells me, that there are two possible candidates for autowiring. The reason for that is, that spring ignores the generics. Is there a solution for that or a workaround?