Followers

springIOC: CI with primitive and String

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

public class Employee {
    private int id;
    private String name;
public Employee(){
    System.out.println("Default constructor...");
}

public Employee(int id){
    System.out.println("Caling Employee(int)");
this.id = id;


public Employee(String name){
    System.out.println("Caling Employee(String)");
this.name = name;


public Employee(int id,String name){
    System.out.println("Caling Employee(int,String)");
    this.id = id;
    this.name = name;
}
public void show(){
    System.out.println(id+" "+name);
}
    
}


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-3.0.xsd">

    <bean id="emp" class="myspring.Employee" >

        <constructor-arg value="100" type="int"></constructor-arg> 
        <constructor-arg value="Hari" type="String"></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