Followers

springIOC: CI with properties

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

import java.util.Properties;

public class Employee {
private String question;  
private Properties properties;
public Employee() {
    System.out.println("default constructor");
}  
  
public Employee( String question, Properties properties) {   
      
    this.question = question;  
    this.properties = properties;  
}  
  
void show(){ 
    
    System.out.println(question);
    System.out.println(properties); 
    System.out.println(properties.get("Mohan"));
    

}   
}


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 are you staying? "> </constructor-arg>
        <constructor-arg>
            <props>
            <prop key="Mohan">INDIA</prop>
            <prop key="Hari">UK</prop>
            <prop key="Thomas">USA</prop>
            <prop key="Hari" >USA</prop>
            </props>
        </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 = context.getBean("emp",Employee.class);// Type casting is not required.
          emp.show();  
    }
    
}

No comments:

Post a Comment