I have one class that has a list of objects of Daemon type.
class Xyz {
List<Daemon> daemons;
}
My spring configuration looks like this.
<bean id="xyz" class="package1.Xyz">
<property name="daemons" ref="daemonsList">
</bean>
<bean id="daemon1" class="package1.DaemonImpl1"/>
<bean id="daemon2" class="package1.DaemonImpl2"/>
<bean id="daemonsList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="daemon1" />
<ref bean="daemon2" />
</list>
</constructor-arg>
</bean>
Now instead of explicitly wiring each daemon implementation in list, is it possible to autowire all beans of type Daemon automatically in list. Problem I am trying to solve is, If someone creates a bean of new implementation of Daemon class and forgets to wire it into list.
I have seen this question somewhere on stackoverflow but not able to find that again. Apologies for it.
It should work like this (remove the ArrayList bean from your XML):
I don’t think there’s a way to do this in XML.
See:
3.9.2.
@Autowiredand@Inject:BTW, as of Spring 4.x, these lists can be ordered automatically using the
@Orderedmechanism.