Reputation: 5032
I have the following scenario:
class Super{
private List<String> someStringsThatWillBeDifferentForEveryInstancePerDerivedType;
}
@Component
class Derived1 extends Super{
@Autowired
private String name;
}
@Component
class Derived2 extends Super{
@Autowired
private Long configId;
}
I have a different List
defined as Spring bean in xml for each derived class…call them listForDerived1
and listForDerived2
. How can I wire these lists into my derived classes? I attempted constructor injection but I can't seem to find any luck injecting both the collection and the other deps.
Upvotes: 0
Views: 760
Reputation: 750
You can use constructor injection with @Qualifier
.
class Super {
private List<String> someStrings;
public Super(private List<String> someStrings) {
this.someStrings = someStrings;
}
}
@Component
class Derived1 extends Super {
@Autowired
public Derived1(@Qualifier("listForDerived1") List<String> listForDerived1, OtherBean bean) {
super(listForDerived1);
}
}
@Component
class Derived2 extends Super {
@Autowired
public Derived1(@Qualifier("listForDerived2") List<String> listForDerived1, OtherBean bean) {
super(listForDerived2);
}
}
Also see official Spring doc: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-autowired-annotation-qualifiers
Upvotes: 2