Setter Type Dependency Injection

Spring can take advantage of a property’s setter method to configure the property’s value through setter injection.

Injecting simple values

Bean properties can be configured in Spring using the <property> element. <property> is similar to <constructor-arg> in many ways, except that instead of injecting values through a constructor argument, <property> injects by calling a property’s setter method.

In order to use the setter injection, we have to provide setters and getters on the respective variables.

Following example illustrates Setter Type Dependency Injection.

In the following example we will consider a Employee class which has two properties to construct the name of the person. We will set the property values in Spring IoC Bean Configuration file and the print the name of the person.

1. Create Employee class with Setter methods

Employee.java

package com.kruders.spring;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Employee {
    private String firstName;
    private String lastName;
    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;
    }
}

2. Create Bean Configuration File

Spring-Bean.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.xsd">
 
    <bean id="employee" class="com.kruders.spring.Employee">
        <property name="firstName" value="FirstName"/>
        <property name="lastName" value="LastName"/>
    </bean>
 
</beans>

<property> tag is used to set the value or reference of a property in bean class. This tag has two attributes ‘name’ and ‘value’. ‘name‘ attribute holds the name of the property in bean. In this case there are two properties in Employee bean, ‘firstName’ and ‘lastName’. ‘value‘ attribute holds the value to be set on the property. Spring IoC container will call property setter methods to set the property value.

3. Add Spring dependency

Write the following code in pom.xml to add Spring 3 dependency

<properties>
   <spring.version>3.0.6.RELEASE </spring.version>
</properties>
<dependencies>
 
   <!-- Spring 3 dependencies -->
   <dependency>
       <groupId>org.springframework </groupId>
       <artifactId>spring-core </artifactId>
       <version>${spring.version} </version>
   </dependency>
 
   <dependency>
       <groupId>org.springframework </groupId>
       <artifactId>spring-context </artifactId>
       <version>${spring.version} </version>
   </dependency>
 
</dependencies>

4. Create TestSimpleValues Class

Now Create Test class that contains the code to run the example. The Test class first create BeanFactory instance using our Spring IoC Bean Configuration file (Spring-Bean.xml) and then get the instance of employee bean to call the method getFirstName in it.

TestSimpleValues.java

package com.kruders.spring;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestSimpleValues {
    public static void main(String args[]) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-Bean.xml");
        Employee employee = (Employee)beanFactory.getBean("employee");
        System.out.println("Employee : " + employee.getFirstName());
 
    }
}

The folder structure of the example is shown below in Figure 4.2

Figure 4.2 Figure 4.2

You can download the source code of this example here.

Referencing other beans

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 employee and we will pass the reference of Address bean to Employee bean using Spring IoC Setter Bean Ref Injection.

1. Create Bean Classes

First Create Address bean with Setter methods

Address.java

package com.kruders.spring;
 
public class Address {
    private String city;
    private String country;
    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;
    }
}


Now Create Employee class with Setter methods and Address bean reference.

Employee.java

package com.kruders.spring;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Employee {
    private String firstName;
    private String lastName;
    private Address address;
     
    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;
    }
}

2. Create Bean Configuration File

Spring-Bean.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.xsd">
 
    <bean id="address" class="com.kruders.spring.Address">
        <property name="city" value="New Delhi"/>
        <property name="country" value="India"/>
    </bean>
    <bean id="employee" class="com.kruders.spring.Employee">
        <property name="firstName" value="Carel"/>
        <property name="lastName" value="Staf"/>
        <property name="address" ref="address"/>
    </bean>
 
</beans>

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

<property name=”address” ref=”address”/>

Here ‘ref‘ attribute create the instance of address bean and set to the property of Employee bean.

3. Create TestRefBean Class

Now Create Test class that contains the code to run the example. The Test class first create BeanFactory instance using our Spring IoC Bean Configuration file (Spring-RefBean.xml) and then get the instance of employee bean to call the method getAddress in it.

TestRefBean.java

package com.kruders.spring;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestRefBean {
    public static void main(String args[]) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-RefBean.xml");
        Employee employee = (Employee)beanFactory.getBean("employee");
        System.out.println("Employee City: " + employee.getAddress().getCity());
 
    }
}

The folder structure of the example is shown below in Figure 4.3

Figure 4.3 Figure 4.3

You can download the source code of this example here.

No comments yet.

Leave a Reply