Map based collection example and Properties example

previous-button-hisubmit-hi

Que: -Map based collection example.

  1. Student.java
  2. College.java
  3. bean.xml
  4. ExecuteBean.java
package com.nit.spring;

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

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

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

public class College
{
     private Map <Integer, Student> students;
     public void setStudents (Map<Integer, Student> students)  //HashMap object is created
     {
         this.students=students;
     }
     public Map <Integer, Student> getStudents ()
   {
         return students;
   }
}

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

bean.xml

<beans>
 <bean name=”c” class “com.nit.spring.College”>
  <property name=”students”>
   <map> //knowing that HashMap object have to be create
    <entry key-“1001”>
     <bean id=”s1” class=”com.nit.spring.Student”>
      <property name=”rollno” value=”1001”/>
       <property name=”name” value=”Rama” /> 
< /bean>
< /entry>
   <entry key-“1002”>
    <bean id=”s2” class=”com.nit.spring.Student”>
     <property name=”rollno” value=”1002” />
      <property name=”name” value=”Rahim” />
< /bean>
   < /entry>
    <entry key-“1003”>
     <bean id=”s3” class=”com.nit.spring.Student”>
       <property name=”rollno” value=”1003” />
        <property name=”name” value=”David” />
< /bean>
< /entry>
< /map>
< /property>
< /bean>
< /beans>

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

ExecuteBean

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”);
       Map<Integer,Student> students=college.getStudents ();
       Set <Integer> keys=students.keySet (); //HashSet object created
               int count=1;
             for (Integer k:keys)
             {
                 Student s=(Student) students.get (k);
                 System.out.println (“Student”+count+”details…”);
                 s.displayStudentDetails ();
                 count++;
             }
     }
}

Que: -Properties example

  1. HelloBean.java
  2. bean.xml
  3. Main.java
package com.nit.spring;
import java.util.Propertis;

public class HelloBean
{
     Properties properties;
     public void setProperties (Properties properties)
     {
         this.properties=properties;
     }
       public Properties getProperties ()
       {
           return properties;
       }
}

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

bean.xml

<beans>
     <bean name=”h” class=”com.nit.spring.HelloBean”>
           <property name=”properties”>
                 <props> //indicating the container to create the Properties object
                         <prop key=”name”>Rama< /prop>
                         <prop key=”address”>Hyderabad< /prop>
                 < /props>
           < /property>
     < /bean>
< /beans>

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

public class Main
{
     public static void main (String s []) throws Exception
     {
         BeanFactory beanfactory=new XmlBeanFactory (new FileSystemResource (“collection.xml”));
         HelloBean myBean= (HelloBean) beanfactory.getBean (“h”);
         Properties p=myBean.getProperties ();
         Set (String> keys=p.stringPropertiesNames () ;
         for (String key:keys)
         {
                 System.out.println (“key+”:”+p.getProperties (key));
         }
     }
}

previous-button-hisubmit-hi