logo

Constructor-Based Dependency Injection


Show

Constructor-based dependency injection in Spring is performed, where the object’s constructor is used to inject dependencies. In other words, it is used when the container invokes a class constructor with several arguments. Here, each argument represents a dependency on the other class. In this type of dependency, a constructor is used to inject dependency. Here the subelement of is used for constructor injection.

In constructor-based dependency, a dependency will be performed by the below injections.

  • primitive and String-based values
  • Dependent object (contained object)
  • Collection values etc.

In this useful guide, we are going to perform the injection based on primitive and String-based values.

Injecting Primitive and String-Based Values

We will provide a simple example to inject primitive and string-based values for better understanding. We have created three files that are listed below:

  • Employee.java
  • applicationContext.xml
  • Test.java

Employee.java is a simple class that contains two fields id and name as well as four constructors and one method.

package com.intellinuts;  

public class Employee {  
private int id;  
private String name;  

public Employee() {System.out.println("def cons");}  

public Employee(int id) {this.id = id;}  

public Employee(String name) { this.name = name;}  

public Employee(int id, String name) {  

    this.id = id;  

    this.name = name;  

}  

void show(){  

    System.out.println(id+" "+name);  

}  

}  

In applicationContext.xml are providing the information into the bean. Here, the constructor-arg element invokes the constructor. ApplicationContext.xml will invoke the parameterized constructor of int type. The value attribute of the constructor-arg element will assign the specified value. The type attribute specifies that the Int parameter constructor will be invoked.

<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="e" class="com.intellinuts.Employee">  
<constructor-arg value="10" type="int"></constructor-arg>  
</bean>  

</beans> 

Test.java class gets the bean from the applicationContext.xml file and calls the below method:


package com.intellinuts;  

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  

public class Test {  
    public static void main(String[] args) {  

        Resource r=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(r);  

        Employee s=(Employee)factory.getBean("e");  
        s.show();  

    }  
}  

The outcome of the program will be 10 null.

Injecting String-Based Values

Here, in this type of injection, by default string type constructor will be invoked in case you don't specify the type attribute in the constructor-arg element. See the below code:

....  
<bean id="e" class="com.intellinuts.Employee">  
<constructor-arg value="10"></constructor-arg>  
</bean>  
....  

Here, if we change the bean element above specified, the string parameter constructor will be invoked and the output will be something like this > 0 10

Here, string literals may also be passed by using the below code:

....  
<bean id="e" class="com.intellinuts.Employee">  
<constructor-arg value="Sonoo"></constructor-arg>  
</bean>  
.... 

Now the output is like this:

0 Sonoo

Further, you may also pass both the integer literal and string by using the code below:

....  
<bean id="e" class="com.intellinuts.Employee">  
<constructor-arg value="10" type="int" ></constructor-arg>  
<constructor-arg value="Sonoo"></constructor-arg>  
</bean>  
.... 


Here the outcome will be 10 Sonoo