I have a class “Box” with add method accepting all the fruits:
public class Box {
List <IFruit> fruits;
public void add (IFruit fruit) {
fruits.add(fruit);
}
}
I would like to define with Spring’s applicationContext.xml a singleton instance of this class, which would have all the IFruits implementations added (those appear in a package x.y.fruits, for inst. x.y.fruits.Apple).
The first part is easy:
<bean id="box" class="x.y.Box"/>
But how to wire all the IFruit instances?
Thanks!
If you
@Autowirethe field, you do not need to define anything, Spring will find all instances of theIFruitinterface in the application context and load them in.Of course, you need to add the element
<context:annotation-config/>to your xml configuration for@Autowiredto work…