Followers

springIOC: CI with Map

Employee.java
==============
package myspring;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Employee {
private String question;  
private Map<String,String> addresses;
public Employee() {
    System.out.println("default constructor");
}  
  
public Employee(String question, Map<String,String> addresses) { 
    this.question = question;  
    this.addresses = addresses;  
}  
  
void show(){
    System.out.println(question);
    Set<Entry<String,String>> set = addresses.entrySet();
    Iterator<Entry<String,String>> itr = set.iterator();
    while(itr.hasNext()){
        Entry<String,String> entry = itr.next();
        System.out.println(entry.getValue()+" is staying in "+entry.getKey());
    
    }
      
    

}   
}


Beans.xml
==========
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/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-4.0.xsd">

    <bean id="emp" class="myspring.Employee" > 
        <constructor-arg value="Where you Guys are staying? "> </constructor-arg>
        <constructor-arg>
            <map>    
                <entry key="Bengaluru" value="Hari" ></entry>      
                <entry key="Chennai" value="Krishna" ></entry>      
                <entry key="Pune" value="Deerubai" ></entry>      
            </map>
        </constructor-arg>
    </bean>


</beans>

Test.java
==========
package myspring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
          Employee emp = (Employee)context.getBean("emp");
          emp.show();
    }
    
}

No comments:

Post a Comment