Share on Google+Share on Google+

Spring Wire Array Type To List Element Example

In this tutorial you will learn about the Wire Array Type To List Element

Spring <list> Configuration Element to wire Array type property

The list element is used to store multiple values.

Its sub elements, which can be used to set values to the list, are as follows:

1. <value> element can be used to set simple values like string

2. <ref> element can be used to provide values as references to other beans and

3. <null> can be used if you want to set null value.

Lets take an example to demonstrate how list element is used. This class contains parts attribute which is of String array type.

import java.util.*;

public class Product
private String[] parts;

public void setParts(String[] parts) { = parts;
public String[] getParts() {
return parts;


spring-beans-list.xml: The <list> element is used to provide values to the String arry type property of the Product class. Here <value> element provides those values.

<beans xmlns=""

<bean id="product" class="Product">
<property name="parts">
<value>Part A</value>
<value>Part B</value>
<value>Part C</value>
</beans> This main class loads the context configuration file "spring-beans-list.xml" from the class path. Its product bean is used to get property of String array type.

import org.springframework.context.ApplicationContext;

public class AppMain
public static void main( String[] args )
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"spring-beans-list.xml"});

Product product = (Product)appContext.getBean("product");

for (String part : product.getParts()) {


Posted on: April 5, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null