Followers

springIOC: Multiple Beans using XML (Type2)

Address.java
=============

package myspringIOC;

public class Address {    
private String city;  
private String state;  
private String country;  

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


  
public String toString(){  
    return city+" "+state+" "+country;  
}  
    
}

Employee.java
==============

package myspringIOC;

public class Employee {
private int id;  
private String name; 
    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 String toString(){  
   return id+" "+name;
    
}   
}

AddressBeans.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="helloAddr" class="myspringIOC.Address">
      <property name="city" value="Bengaluru"/>
      <property name="state" value="Karnataka"/>
      <property name="country" value="India"/>
   </bean>

   

</beans>

EmployeeBeans.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="helloEmp" class="myspringIOC.Employee">
      <property name="id" value="1222"/>
      <property name="name" value="Hari"/>
   </bean>

</beans>

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">
    
    <!-- import all the Bean configuration files here. -->
    <import resource="AddressBeans.xml" ></import>
    <import resource="EmployeeBeans.xml" ></import>


</beans>

Test.java
==========
package myspringIOC;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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("helloEmp",Employee.class);
        Address  addr = context.getBean(Address.class);// we cam mention directly class if that has ony one bean.
        System.out.println(emp);
        System.out.println(addr);
        
    }
    
}

No comments:

Post a Comment