Followers

springIOC: SI 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 String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }
/*
    public Map<String, String> getAddresses() {
        return addresses;
    }
*/

    public void setAddresses(Map<String, String> addresses) {
        this.addresses = addresses;
    }

  
  
void show(){
    System.out.println(question);
    System.out.println("============================");
    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" >
        <property name="question" value="Where you Guys are staying? "> </property>
        <property name="addresses">
            <map>    
                <entry key="Bengaluru" value="Hari" ></entry>      
                <entry key="Chennai" value="Krishna" ></entry>      
                <entry key="Pune" value="Deerubai" ></entry>      
            </map>
        </property>
    </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 = context.getBean("emp",Employee.class);
          emp.show();
    }
}

No comments:

Post a Comment