Hello I have a problem in my project:

// this is parent interface
public interface ParentComponent<T extends TestParent> {
    void test(T test);
}
// impl 1
@Component
public class SonComponent1 implements ParentComponent<TestSon1> {
    @Override
    public void test(TestSon1 test) {
        System.out.println(test);
    }
}
// impl 2
@Component
public class SonComponent2 implements ParentComponent<TestSon2> {
    @Override
    public void test(TestSon2 test) {
        System.out.println(test);
    }
}
// parent class
public class TestParent {
}
// son1
public class TestSon1 extends TestParent {
}
// son2
public class TestSon2 extends TestParent {
}

I want inject like this. What should I do?

@Autowired
private List<ParentComponent<TestParent>> parentComponents;

I don't want to use like below.

@Autowired
ParentComponent<TestSon2> son2;

@Autowired
ParentComponent<TestSon1> son1;

Comment From: sbrannen

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Comment From: sbrannen

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.


The above is our standard policy for such questions.

In any case, the following is the solution to your issue.

@Autowired
private List<ParentComponent<? extends TestParent>> parentComponents;