Followers

springIOC: SI with properties

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

import java.util.Properties;

public class Employee { 
private String question;  
private Properties properties;

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

  
  
void show(){
    System.out.println(question);
    System.out.println("============================");
    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" >
        <property name="question" value="Where you Guys are staying? "> </property>
        <property name="properties">
            <props>
            <prop key="Mohan">INDIA</prop>
            <prop key="Hari">UK</prop>
            <prop key="Thomas">USA</prop>
            <prop key="Hari">USA</prop>
            </props>
        </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