Inversion of Control(IoC) and Dependency injection(DI) in Spring(JAVA)

In this article, we will discuss Inversion of Control and Dependency Injection in detail.

Introduction:

Inversion of control and dependency injection is part of the spring Core, and both related to each other. The main objective is to make a Tightly Coupled java application into a Loosely Coupled application through injecting dependencies into a component when it is constructed. These dependencies are usually provided as interfaces for further decoupling and to support testability.

Spring IOC:

The spring Ioc is a container that contains two sub-containers, one is a core container and another one is a J2EE container. The core container is the BeanFactory container and the J2EE container is ApplicationContext and Configurable ApplicationContext.

If the IOC container is the core container, then IOC will do the following things.

The container can create instances of POJO classes.

The Container can manage the life cycle of a POJO.

The container can do dependency injection to the POJO.

Let’s understand the concept of Inversion of Control (IOC) by looking at an example. You have an application that will give you the recipe for a dish. And there are two requirements two fulfill the IOC principle,

  1. Easily change the recipe for another dish.
  2. My app should be configurable.
Both are Tightly coupled
  • In the first image, we are directly creating an instance of the PizaRecipe class and calling the getRecipe method. So here we are not achieving the first requirement itself.
  • In the second image, To achieve the first requirement I'm using the HAS-A relationship. So you can switch easily from one dish class to another. But still, here we are hard coding so it's not configurable.
example for Loosely Coupled
  • Here you just need to add all your required object in configuration file then at runtime, your object will be created by the object factory.
  • That object factory will inject that object into our application, so here you no need to hardcode your classes just add your required class the bean factory will generate for you.

Dependency Injection

  • Dependency Injection is the main heart of the Spring framework, the functionality of Spring Dependency Injection is to remove the dependencies in the code.
  • The advantage of dependency injection is to make the code loosely coupled, it overcomes the drawbacks of dependency lookup, dependency lookup is the way where developers get the resource after the demand.
  • Dependency lookup makes the code tightly coupled means that once the resource is changed, the developer has to do more changes in the code, testing of an application is also not possible, but these problems can be removed by dependency injection.
  • In the Spring framework dependency injection will be performed in two ways as follows.
  1. Constructor Based Dependency Injection.

2. Setter Based Dependency Injection.

Constructor Based Dependency Injection

<bean id=”foo” class=”com.pavan.PizzaRecipe” name=”recipe”>

<constructor-arg ref=”bar” />

</bean>

<bean id=”bar” class=”com.pavan.Chief” name=”chief”>

<constructor-arg value=”Sai” />

<constructor-arg value=”22” type=”int”/>

</bean>

  • Here injection is performed by calling the constructor in the class mention ‘com.pavan.PizzaRecipe’.
  • IOC container will use this class parameterized constructor to inject these values.
  • The first value is injected in the first parameter mentioned and next so on.
  • By default every value mention in the configuration file is converted into String, we can explicitly define the type of the variable.

Setter Based Dependency Injection

<bean id=”bar” class=”com.pavan.PizzaRecipe” name=”recipe”>

<property name=”chiefName” ref=”foo” />

</bean>

<bean id=”foo” class=”com.pavan.Chief” name=”chief”>

<property name=”name” value=”Sai” />

<property name=”age” value=”22” type=”int”/>

</bean>

  • Here injection is performed by calling setter method in the class mention ‘com.pavan.PizzaRecipe.
  • IOC container will use the class’s setter method to inject these values.
  • Value is injected in the variable name mentioned.

Note:

  • Only primitive values we use “value attribute”.
  • For Injecting object we should use “ref attribute”.
  • Id of bean should be unique, no duplicates allowed.

As mentioned above, Spring Inversion of Control(IoC) helps in the creation of loosely coupled applications because of Dependency Injection. By implementing Inversion of Control, a software/object consumer gets more controls/options over the software/objects, instead of being controlled or having fewer options.

Passion towards gaining knowledge