Friday, 9 September 2016

Spring Framework DI: Advantages, Types and Examples

Post Brief Table of Content

  • Introduction
  • DI (Dependency Injection)  Basics
  • Advantages of DI
  • Types of DI
  • Setter Injection (Setter DI)
  • Constructor Injection (Constructor DI)
  • Interface Injection (Interface DI)
  • DI Interview Questions

Introduction

Before reading this post, please go through my previous posts: (click on the links)

In this post, I'm going to discuss some more basic principles of Spring Framework. 

DI (Dependency Injection) Basics

DI stands for Dependency Injection. DI is one of the form of IOC. Spring Framework was designed based on this pattern. 

As we discussed in my previous post, DI means “Resolving the bean dependencies”. In Spring Applications, Spring IOC Container resolves these dependencies automatically.


NOTE:- IOC and Dependency Injection follow Push Model.
Advantages of DI
In this section, we will discuss about what are all the major advantages of DI.

Because of DI follows Push Model, it has the following advantages:
  • Developer is not responsible for creating of objects
  • Reduces the application complexity
  • Reduces development time
  • Easy to do the application unit testing
  • Decreases components dependencies
NOTE:- What is the main responsibility of DI?

Object Dependencies Management

Types of DI

DI (Dependency Injection) can be divided into the following three main categories:
  • Setter Injection
  • Constructor Injection
  • Interface Injection



We will discuss these categorizes one by one in detail with suitable examples in the coming sections.

Setter Injection (Setter DI)

As we know, DI means resolving bean dependencies. To resolve bean dependencies, we can use different techniques.

If we use setter method to resolve bean dependencies, then this kind of DI (Dependency Injection) is known as Setter Injection or Setter DI.

Setter DI Example:-
public class CardService
{
    private CreditCard creditCard;
    // Setter 
    public void setCreditCard(CreditCard creditCard)
    {
 this.creditCard= creditCard;
    }
}
How to represent Setter DI in Spring Bean Configuration XML file?
By using <property> element
Syntax:-
For Primitive or String Properties:
<property name="property-name"  value=”some-value”/>

Syntax:-
For Reference Properties:
<property name="property-name"  ref=”reference-name”/>
Example:-
<bean id=”cardService”  class=”com.way2it.bank.CardService”>
 <property  name=” creditCard”   ref=” creditCard”/>
</bean>
Constructor Injection (Constructor DI)

If we use constructor to resolve bean dependencies, then this kind of DI (Dependency Injection) is known as Constructor Injection or Constructor DI.

Constructor DI Example:-
public class CardService
{
   private CreditCard creditCard;
   // Constructor  
   Public CardService (CreditCard creditCard)
   {
     this.creditCard= creditCard;
   }
}

How to represent Constructor DI in Spring Bean Configuration XML file?
By using <constructor-arg> element, but not <property> element
Syntax:-
For Primitive or String Properties:
<constructor-arg  value=”some-value”/>

Syntax:-
For Reference Properties:
<constructor-arg  ref=”refBeanObject”/>
Example:-
<bean id=”cardService”  class=”com.way2it.bank.CardService”>
 <constructor-arg  ref=” creditCard”/>
</bean>
Interface Injection (Interface DI)

If we use interface to resolve bean dependencies, then it is known as Interface Injection or Interface DI.

How to implement Interface Injection?
Spring framework does NOT support Interface Injection.

Why Spring framework does NOT support Interface Injection?
Because we can NOT instantiate an Interface.

That’s it all about “Spring Framework DI” concept. We will discuss some more important Spring Basics in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.