What is Dependency Injection :

Dependency injection is also called as Inversion of control. The Inversion of control is a design pattern describing an external entity. It is used to write objects at creation time by injecting their dependencies, so that they can work together in a system.

In other words the IoC describes that a dependency injection needs to be done by an external entity instead of creating the dependencies by the component itself. Dependency injection is a process of injecting (pushing) the dependencies into an object.

Why Dependency Injection :

In developing huge systems using the OOP methodology, we generally divide the system into objects, where each of the objects represents some functionally. In this case, the objects in the system use some other objects to complete the given request. The objects with which our object collaborates to provide the services are known as its dependencies, the traditional ways of obtaining the dependencies are by creating the dependencies or by pulling the dependencies using some factory classes or methods or from the naming registry.

Because of these approaches result in:

  • The complexity of the application increases.
  • The development time-dependency increases.
  • The difficulty for the unit testing is increases.

Types of Dependency Injection:

There are two types of dependency injection in spring :

  • Constructor Injection and
  • Setter Injection

Constructor Injection:

In the Constructor Injection method, the dependencies of an object are injecting through its constructor arguments. In this mechanism the dependencies are pushed in to the objects through the constructor arguments at the time of initializing it.

<constructor-arg> element :

<constructor-arg> tag is used to pass the parameters to constructor. The <constructor-arg> element describes one argument constructor, which means that to specify a constructor with multiple arguments we need to use the multiple <constructor-arg> elements.

The possible attributes of the <constructor-arg> tag :

Type : The type attribute is used to define the data type of the constructor parameter. This is used to void ambiguity between the two constructors.

Index : The index attribute is used to take the exact index in the constructor argument list. This is used to avoid ambiguities like, in case of two arguments are being the  same type.

Name : The name attribute is used to specify the name of the constructor argument.

Example:

Employee.java

Employee.java
package com.onlinetutorialspoint.springcoreapplication;

public class Employee {
    private int employeeId;
    private String employeeName;

    public Employee(int employeeId, String employeeName) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

}

xml configuration:

Spring-Bean.xml

Spring-Bean.xml
<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-2.5.xsd">               
    <bean id="employee" class="com.onlinetutorialspoint.springcoreapplication.Employee">                           
        <constructor-arg value="200" />                           
        <constructor-arg value="chandrashekhar" />               
    </bean> 
</beans>

Run the application

Client.java

Client.java
package com.onlinetutorialspoint.springcoreapplication;

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

public class Client {

public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-Bean.xml");
Employee employee = (Employee) beanFactory.getBean("employee");
System.out.println("employee Id : " + employee.getEmployeeId());
System.out.println("employee Name : " + employee.getEmployeeName());
}
}

Output:

Constructor injection

Setter Injection :

Setter injection is one of the most commonly used approaches of dependency injection, By using the setter injection method  the dependencies of the object  can be injected through the setter methods.

Example:

Employee.java

Employee.java
package com.onlinetutorialspoint.springcoreapplication;
public class Employee {
    private int employeeId;
    private String employeeName;

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

}

Xml configuration:

Spring-Bean.xml

Spring-Bean.xml
<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-2.5.xsd">     
    <bean id="employee" class="com.onlinetutorialspoint.springcoreapplication.Employee">         
        <property name="employeeId" value="200" />         
        <property name="employeeName" value="Chandrashekhar" />     
    </bean> 
</beans>

Run the application,

Client.class

Employee.java

package com.onlinetutorialspoint.springcoreapplication;

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

public class Client {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("Spring-Bean.xml");
        Employee employee = (Employee) beanFactory.getBean("employee");
        System.out.println("employee Name : " + employee.getEmployeeName());
    }
}

Output:

Setter Injection

Download Example