set based collection example.

previous-button-hisubmit-hi

Que: -set based collection example.

  1. College.java
  2. bean.xml
  3. ExecuteBean.java
  4. Student.java

College.java

package com.nit.spring;
import java.util.set;

public class College
{
private String name;
private Set <Student>students;
public College (String name)
{
this.name=name;
}
public void setStudents (Set students)
{
this.students=students;
}
public Set getStudents ()
{
return students;
}
}

————————————————————

Student.java

package com.nit.spring;

public class Student
{
private int rollno;
private String name;
public Student (int rollno, String name)
{
this.rollno=rollno;
this.name=name;
}
public void displayStudentDetails ()
{
System.out.println (“Rollno:”+ rollno);
System.out.println (“Name:”+ name);
}
}

————————————————————

bean.xml:

< ? xml version=”1.0” encoding=”UTF-8”?>
<beans>
xmlns= “http://www.springframework.org/shema/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.0.xsd”>
<bean id= “s1” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1001” />
<constructor-arg value=”Rama” />
< /bean>
<bean id= “s2” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1002” />
<constructor-arg value=”Rahim” />
< /bean>
<bean id= “s3” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1003” />
<constructor-arg value=”David” />
< /bean>
<bean name= “c” class= “com.nit.spring.Student”>
<constructor-arg value=”Rama” />
<property name=”students”>
<set>
<ref bean=”s1” />
<ref bean=”s2” />
<ref local=”s3” />
<ref local=”s3” />
< /set>
< /property>
< /bean>
< /beans>

————————————————————

public class ExecuteBean
{
public static void main (String args[]) throws Exception
{
BeanFactory beanfactory=new XmlBeanFactory (new FileSystemResource (“collection.xml”));
College college= (College) beanfactory.getBean (“c”);
Set <Student> students=college.getStudents ();
int count=1;
for (Student s:students)
{
System.out.println (“Student”+count+”details…”);
s.displayStudentDetails ();
count++;
}
}
}

previous-button-hisubmit-hi