Followers

springIOC : autowire byName

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

package myspringIOC;

public class Address {    
private String city;  
private String state;  
private String country; 
public Address(){
    System.out.println("Address default constructor...");
}

    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;  
private Address address;
public Employee(){
System.out.println("Employee default constructor...");
}

    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 Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        System.out.println("Calling setAddress");
        this.address = address;
    }

  
void show(){  
    System.out.println(id+" "+name);  
    System.out.println(address); 
}   
}

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="myspringIOC.Employee" autowire="byName"> 
        <property name="id" value="1111"></property>
        <property name="name" value="Thomas"></property>
        <!--<property name="address" ref="addr"> </property>  -->

    </bean>
    <bean id="address" class="myspringIOC.Address" > 
        <property name="city" value="Bengaluru">   </property>
        <property name="state" value="Karnataka">   </property>
        <property name="country" value="India">   </property>    
    </bean>
    <!--
    <bean id="address12" class="myspringIOC.Address" > 
        <property name="city" value="Chennai">   </property>
        <property name="state" value="TN">   </property>
        <property name="country" value="India">   </property>    
    </bean>
      -->
    
</beans>

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

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