Spring Set Property
In this tutorial you will learn about the spring set property.
In this tutorial you will learn about the spring set property.
Spring Set Property
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 set
elements which is used to set values inside the set.
CollegeBean.java
package collection.set.example;
import java.util.Sets;
public class CollegeBean {
private Set<Object> sets;
public Sets<Object> getSets() {
return sets;
}
public void setSets(Set<Object> sets) {
this.sets = sets;
}
@Override
public String toString() {
return "College [sets=" + sets + "]";
}
}
|
StudentBean.java
package collection.set.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.set.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 = (College) beanfactory.getBean("collegeBean1");
System.out.println(coll);
}
}
|
context.java
<!-- Spring Sets Property Example -->
<bean id="studentBean1" class="collection.set.example.StudentBean">
<property name="name" value="satya" />
<property name="address" value="Delhi" />
</bean>
<bean id="collegeBean1" class="collection.set.example.CollegeBean">
<property name="sets">
<list>
<value>1</value>
<ref bean="studentBean1" />
<bean class="collection.set.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 [sets=[1, Student [address=Delhi,
name=satya], Student [address=delhi, name=ankit]]] |
Download this example code