Spring IoC Setter Bean Ref Injection

In this session we will go through an example that will make you understand Spring IoC Setter Bean Ref Injection. Bean ref injection means to to pass reference of one bean to another. So the example will show the method of setting one bean reference to another bean in Spring IoC Bean Setter Injection.

In the following example, there is a Address bean to store address of a person and we will pass the reference of Address bean to Person bean using Spring IoC Setter Bean Ref Injection.

Address bean (Address.java)

Address bean has two properties and getter/setter of the properties to store city and country of a person.


package com.raistudies.beans;

    public class Address {
    private String city = null;
    private String country = null;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "City : " + city + ", Country" + country;
    }
}

Person bean code (Person.java)

Person bean contains three properties “firstName”, “lastname” and “address” and their getter and setter.


package com.raistudies.beans;

public class Person {
    private String firstName = null;
    private String lastName = null;
    private Address address = null;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String getName(){
        return firstName + " " + lastName;
    }

    public String getPersonDetail(){
        return "Name :" + getName() + "\n" + address.toString();
    }
}

Spring IoC Bean Configuration file(app-config.xml)

We have configure both the beans Address and Person in our Spring IoC Bean Configuration file.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="address" class="com.raistudies.beans.Address">
        <property name="city" value="New York"/>
        <property name="country" value="USA"/>
    </bean>
    <bean id="person" class="com.raistudies.beans.Person">
        <property name="firstName" value="Carel"/>
        <property name="lastName" value="Staf"/>
        <property name="address" ref="address"/>
    </bean>
</beans>

You can see only one new thing from previous tutorial.

<property name=”address” ref=”address”/> : Here “ref” attribute says to Spring that Spring has to create the instance of address bean and set to the property of person bean.

SpringBeanTestRunner class to test the example

SpringBeanTestRunner class will get Person instance from Spring IoC Container and will call the method “getPersonDetail” method which will show the Name and Address of the person to test the example.


package com.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.raistudies.beans.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        System.out.println("Initialing bean factory");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("app-config.xml");
        System.out.println("Getting person bean");
        Person person = (Person)beanFactory.getBean("person");
        System.out.println("Person detail is " + person.getPersonDetail());
    }
}

Import the code in Eclipse and run, you will get following output:

Spring IoC Setter Bean Ref Injection Output

Spring IoC Setter Bean Ref Injection Output

You can download source or full project using following links to test the example implementation of Spring IoC Setter Bean Ref Injection.

Source : Download

Source + lib: Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>