Wiring Lists and Arrays in Spring Beans
by, 11-16-2011 at 05:17 PM (3673 Views)
Often you will have situations where you will need to wire properties that are lists or arrays. If you are using Hibernate jointly with Spring, you will need to se this in order to provide the list of Hibernate mapping files. In order to do this, you will use the <list> element to wire the property in the wiring XML file for either an array property or a java.util.List property.
For example, when you want to map Hibernate mapping files, you will wire a LocalSessionFactoryBean into the container. The LocalSessionFactoryBean has a mappingResources property that takes a List of Strings containing the names of Hibernate mapping files. Below is an example of how you would use a list in conjunction with Hibernate:
Remember that you can use other types than a List of String values. You can use any valid element when you are wiring a singular property, including values (<value>), references (<ref>) or even lists (<list>). You must ensure that the list aligns with the expected type of your bean.Java Code:<bean id="sessionFactory" class= "org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value>/com/acme/springexamples/model/Car.hbm.xml</value> <value>/com/acme/springexamples/model/Engine.hbm.xml</value> <value>/com/acme/springexamples/model/Wheels.hbm.xml</value> </list> </property> </bean>