Sunday 20 March 2011

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.

Example

Consider this list bean :

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 + "]";
}
}


Simplebean


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 + "]";
}
}


context.xml


<!-- Spring List Property Example  -->

<bean id="studentBean" class="com.xxx.StudentBean">
<property name="name" value="satya" />
<property name="address" value="Delhi" />
</bean>

<bean id="collegeBean" class="com.xxx.CollegeBean">

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

<!-- End -->

No comments:

Post a Comment