Followers

springIOC: SI with collection

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

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Employee {
private int id;  
private String name;  
//private List<String> address;
private Set<String> address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
/*
    public List<String> getAddress() {
        return address;
    }

    public void setAddress(List<String> address) {
        this.address = address;
    }
*/

    public Set<String> getAddress() {
        return address;
    }

    public void setAddress(Set<String> address) {
        this.address = address;
    }
    
    
    
    
void show(){  
    System.out.println("Employee number = "+id);
    System.out.println(name);
      
    
    Iterator<String> itr = address.iterator();
    while(itr.hasNext()){
        System.out.println(itr.next());
    
    }

}

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="id" value="111" > </property>
        <property name="name" value="Where are you staying? "> </property>
        <property name="address">
            <set>          
                <value>Munnekolala</value>
                <value>Marthalli</value>
                <value>Bengaluru</value>
                <value>Bengaluru</value>
            </set>
        </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 = (Employee)context.getBean("emp");
        emp.show();
        
    }
}

No comments:

Post a Comment