Constructor Type Dependency Injection

This Dependency Injection method will injects the dependency via a constructor. That means the constructor of the bean must be defined to accept attributes values and set the values to bean properties.

Following example illustrates Construction 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 employee.

1. Create Employee class with Constructor

Employee.java

package com.kruders.spring;
 
public class Employee {
    private String firstName;
    private String lastName;
     
    public Employee(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = 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">
        <constructor-arg  name="firstName" value="FirstName"/>
        <constructor-arg  name="lastName" value="LastName"/>
    </bean>
 
</beans>

<constructor-arg/> tag is used to set constructor parameter values in Spring IoC container. ‘name‘ attribute is used to specify the name of the constructor parameter and ‘value‘ attributes says the value to be set on it.

There are 3 ways of injecting values through a constructor argument.

Specify Name of the argument

<bean id="employee" class="com.kruders.spring.Employee">
    <constructor-arg  name="firstName" value="FirstName"/>
    <constructor-arg  name="lastName" value="LastName"/>
</bean>

Specify Type of the argument

<bean id="employee" class="com.kruders.spring.Employee">
    <constructor-arg  type="java.lang.String" value="FirstName"/>
    <constructor-arg  type="java.lang.String" value="LastName"/>
</bean>

Specify Index of the argument

<bean id="employee" class="com.kruders.spring.Employee">
    <constructor-arg  index="0" value="FirstName"/>
    <constructor-arg  index="1" value="LastName"/>
</bean>

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 TestConstructor 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.

TestConstructor.java

package com.kruders.spring;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestConstructor {
    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.1

Figure 4.1 Figure 4.1

You can download the source code of this example here.


No comments yet.

Leave a Reply