Collection Bean in Spring
Another easy task that can seem hard
A task that seems to come up fairly often is creating a bean that is a collection. We have covered how to create a collection property and now we will show just how easy it is to create a collection bean.
The trick is to remember that there are constructors to most collection classes that take another collection as a constructor argument. Since constructor arguments accept the same tags as properties, the solution is to create a constructor argument.
<bean id="beanId" class="java.util.ArrayList">
<constructor-arg>
<list>
<bean class="..." >...</bean>
...
</list>
</constructor-arg>
</bean>
A frequent error is trying to use a Collection that is an interface (like List) instead of a concrete class. Once you create the bean, it can be used like any other. Remember that if more than one bean uses the collection, they will all be affected if the contents of the collection change.