Posts

Showing posts from November, 2010

Loading a List from a file in Spring Application Context

Have you ever wanted to load a List in your application context from a file? Perhaps you want to define a variable number of settings to be injected in, or in my case I wanted to pull in the Hibernate mapping files into an AnnotationSessionFactoryBean. Spring doesn't appear to have a way to do this out of the box, so here is how I have done it. So you want to load a list of strings defined in a file called names.txt such as: James Adam Peter Paul Your application context would look something like: <bean id="resourceList" class="com.fizzpod.spring.ResourceListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="locations"> <list> <value>classpath:names.txt</value> </list> </property> </bean> The ResourceListFactoryBean is (obviously) a factory bean for creating a list from a Spring resource. Now this