Dependency Injection

Dependency Injection (DI) is also known as Inversion of Control (IOC).

In traditional Java application classes are connected to each other, in other words classes are dependent on each other. That makes limited re-usability of the classes. Spring provides DI functionality of the programming. The main concept of DI is to make the classes independent from each other to increase their re-usability, while keeping them together at a run time.

Benefits of Dependency Injection in Spring
  • Reduces dependency of classes with each other.
  • Code can be clean and more readable.
  • Because all configurations are in .XML file, can switch implementation by just changing configuration file.
  • Makes easier to test the code using various test modules.
  • Increases the modularity and re-usability of code.

There are two types of Dependency Injections:
  1. Setter based Dependency Injection: Setter based Dependency Injection can be achieved by calling setter method of the class after calling no-argument constructor or no-argument bean factory method.
  2. Constructor based Dependency Injection: Constructor based Dependency Injection can be achieved by passing an argument into constructor while injecting an object into client class. 

Setter Based DI Example: Here is the code for Class First and Class Second without Dependency Injections. 

Class First:
public class First {
public void printHello(){
System.out.println("Hello!!");
}
}

Class Second:
public class Second {
public void printHello(){
First a = new First();
a.printHello();
}
}

In above example, Class Second is too much dependent on Class First. It is also known as "Tight Coupling". In this case Class Second is always dependent on Class First, as it has to create instance of Class First everytime. So, using this method there is not much modularity and re-usability of the code. Spring provides the dependency injection, by this means, Spring will inject the instance of the Class First into the Class Second when Spring's container get initialized at run time. So, Class Second does not have to wait for Class First to provide its  instance all the time. Here is the modified code for Class Second using Dependency Injection:

Class Second:
public class Second {
 private First a;

 public void setFirst(First a){
    this.a = a;
 }

public void printHello(){
a.printHello();
}
}
As in above example, setFirst method is used to inject the instance of Class First. This method of injection is also called as "Setter Based Dependency Injection".


Constructor Based DI Example: Consider the same code for Class First and Class Second, without Dependency Injection in setter based DI injection. Now, here is the code for Class Second with Dependency Injection.

Class Second:
public class Second {
 private First a;

 public Second(First a){
   this.a = a;
}
public void printHello(){
a.printHello();
}
}

In above example of Class Second, object of Class First is passed into a constructor to inject the object of Class First into the Class Second. Because, it is using constructor this method is called as "Constructor Based Dependency Injection".

This is the basic concept of Dependency Injection, a third party framework is injecting the instance of object into client object.

This is not done for the Java Spring. To configure dependency injections for Java Spring, There are some more configurations to do in XML file, depending on what type of Dependency Injection is used in the application.

When to use Setter Based DI and Constructor Based DI?

Both of the method has its own advantages and disadvantages over each other, such as, Setter based DI has more readability of the code, on the other hand, Constructor based DI has more flexibility with the object. Spring is much more flexible about the method used for DI, so, there can be used any method they like or even both methods of DI in the same application.

Generally, Setter based DI is used when there are more dependencies and need readability in the code and Constructor based DI is used when object need to use with all its dependencies.


No comments:

Post a Comment