Followers

springIOC: CI with Collection

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

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

public class Employee {
private int id;  
private String name;  
private List<String> list;
public Employee() {
    System.out.println("default constructor");
}  
public Employee(int id, String name, List<String> list) {    
    this.id = id;  
    this.name = name;  
    this.list = list;  
}  
  
void show(){  
    System.out.println("Employee number = "+id);
    System.out.println(name);     
    
    Iterator<String> itr = list.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" > 
        
        <constructor-arg name="name" value="Where are you staying? "> </constructor-arg>
        <constructor-arg name="id" value="111" type="int" > </constructor-arg>
        <constructor-arg>
            <list>          
                <value>Munnekolala</value>
                <value>Marthalli</value>
                <value>Bengaluru</value>
                <value>Bengaluru</value>
            </list>
        </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