Spring List Property Example


 

Spring List Property Example

In this spring framework tutorial you will see the use of Lisp property.

In this spring framework tutorial you will see the use of Lisp property.

Spring List Property Example

The Spring Framework has bean support for the Collections. It provide list, set, map and props elements. Here in this tutorial you will see about the list elements which is used to set values inside the list.

CollegeBean.java

package collection.list.example;

import java.util.List;

public class CollegeBean {
  private List<Object> lists;

  public List<Object> getLists() {
    return lists;
  }

  public void setLists(List<Object> lists) {
    this.lists = lists;
  }

  @Override
  public String toString() {
    return "College [lists=" + lists + "]";
  }
}

StudentBean.java

package collection.list.example;

public class StudentBean {
  private String name;
  private String address;

  public String getName() {
    return name;
  }

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

  public String getAddress() {
    return address;
  }

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

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

ListMain.java

package collection.list.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ListMain {
  public static void main(String[] args) {
    BeanFactory beanfactory = new ClassPathXmlApplicationContext(
        "context.xml");
    College coll = (Collegebeanfactory.getBean("collegeBean");
    System.out.println(coll);
  }
}

context.java

  <!-- Spring List Property Example  -->

  <bean id="studentBean" class="collection.list.example.StudentBean">
    <property name="name" value="satya" />
    <property name="address" value="Delhi" />
  </bean>

  <bean id="collegeBean" class="collection.list.example.CollegeBean">

    <property name="lists">
      <list>
        <value>1</value>
        <ref bean="studentBean" />
        <bean class="collection.list.example.StudentBean">
          <property name="name" value="ankit" />
          <property name="address" value="delhi" />
        </bean>
      </list>
    </property>
  </bean>

  <!-- End -->

When you run this application it will display output as shown below:

College [lists=[1, Student [address=Delhi, name=satya], Student [address=delhi, name=ankit]]]

Download this example code

Ads