Spring Tutorial - Spring List Properties








We can fill value or a list of values to a Java bean defined in Spring xml configuration file.

The following sections shows how to fill data to List.

Java Bean

In order to show how to use xml configuration file to fill collection properties, we defined a Customer object with four collection properties.

package com.java2s.common;
import java.util.ArrayList;
import java.util.List;
public class Customer 
{
  private List<Object> lists = new ArrayList<Object>();
    public String toString(){
      return lists.toString();
    }
  public List<Object> getLists() {
    return lists;
  }
  public void setLists(List<Object> lists) {
    this.lists = lists;
  }
}

Person Java Bean

package com.java2s.common;

public class Person {
  private String name;
  private int age;
  private String address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", address=" + address
        + "]";
  }
  
}




List

The following code shows how to fill data to a java.util.List typed property.

The code fills in three values. The first one is hard-coded value 1. The second one is a bean reference. We have to define PersonBean somewhere in order to use it here. The third one is a bean definition with property-setting.

...
<property name="lists">
    <list>
      <value>1</value>
      <ref bean="PersonBean" />
      <bean class="com.java2s.common.Person">
        <property name="name" value="java2sList" />
        <property name="address" value="address" />
        <property name="age" value="28" />
      </bean>
    </list>
</property>
...




Example

Full Spring's bean configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <!-- java.util.List -->
    <property name="lists">
      <list>
        <value>1</value>
        <ref bean="PersonBean" />
        <bean class="com.java2s.common.Person">
          <property name="name" value="java2sList" />
          <property name="address" value="address" />
          <property name="age" value="28" />
        </bean>
      </list>
    </property>
  </bean>
  <bean id="PersonBean" class="com.java2s.common.Person">
    <property name="name" value="java2s1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
  </bean>
</beans>

Here is the code to load and run the configuration.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

Output



Download Java2s_Spring_List_Properties.zip

ListFactoryBean

ListFactoryBean class can create a concrete List collection class with ArrayList or LinkedList in Spring's bean configuration file. Then we can inject the created list object to our Java bean.

Here is the Java bean class.

package com.java2s.common;
import java.util.ArrayList;
import java.util.List;
public class Customer 
{
  private List<Object> lists = new ArrayList<Object>();
    public String toString(){
      return lists.toString();
    }
  public List<Object> getLists() {
    return lists;
  }
  public void setLists(List<Object> lists) {
    this.lists = lists;
  }
}

Here is Spring's bean configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 //from w w  w. ja  va 2 s.com
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <property name="lists">
      <bean class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="targetListClass">
          <value>java.util.ArrayList</value>
        </property>
        <property name="sourceList">
          <list>
            <value>1</value>
            <value>2</value>
            <value>3</value>
          </list>
        </property>
      </bean>
    </property>
  </bean>
</beans>


Download Java2s_Spring_ListFactoryBean.zip

util schema

We also can use util schema and <util:list> to fill data to a list.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-2.5.xsd">
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <property name="lists">
      <util:list list-class="java.util.ArrayList">
        <value>1</value>
        <value>2</value>
        <value>3</value>
      </util:list>
    </property>
  </bean>
</beans>


Download Java2s_Spring_util_List.zip

Here is the code to run.

package com.java2s.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "SpringBeans.xml");
 
    Customer cust = (Customer) context.getBean("CustomerBean");
    System.out.println(cust);
 
  }
}