Inversion Of Control and Dependency Injection in Spring

Manish singh rav
4 min readJun 13, 2021

In this article, We will discuss what is Inversion Of Control (IOC) and Dependency Injection (DI). And What are the advantages?

Introduction :

Inversion of control and dependency injection are part of the spring framework, Inversion of control and dependency injection are related to each other.

Inversion of control and dependency injection are used to make our application loosely coupled, Inversion of control is the container that is used in the spring framework, Spring container consists two sub-containers.

  1. Beanfactory
  2. ApplicatonContext

Inversion Of Control (IOC) :

Inversion of control is a container in the spring framework which we use to loosely coupled the objects or remove dependencies. inversion of control can be done by Beanfactory and ApplicationContext.

An inversion of control container creates, configures, and connects the objects and also manages their lifecycle. The container gets instruction for these areas from configuration data given by the user.

Beanfactory container :

This factory class contains a prepackaged collection of beans that instantiate when called by clients. This is the most basic container to support Dependency Injection.

ApplicationContext container :

Built on top of the BeanFactory Container or we can say extended, This container provides additional enterprise-focused functionalities. For example, ApplicationContext containers grant the ability to resolve textual messages and publish application events.

Advantages of inversion of control :

→ By using this we can achieve the clarity in our code.

→ If we need to change some functionality, Then we don’t need to rewrite the whole code or make change in whole file .

→Forces you to write more modular code

→We can re-use the classes.

→It provides more simple structure of application.

→ control over lifetime of dependencies

→Decouples the application

Dependency Injection (DI) :

Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container injects objects into other objects.

Simply we can say, This allows for loose coupling of components and provides the responsibility of managing components onto the container.

For example we are building the application where we’ve more then one class and that consists variables, and that class depends on another class or its objects. So we can perform dependency injection on this.

Dependency injection we can use mostly in two ways.

  1. Setter injection
  2. Constructor injection

Setter injection :

In this process we provide data to the object using setter methods

Here we’ve created a class in which we are using setters for setting the data.

Now let’s create a configuration file where we create bean.

This is our configuration file (config.xml) where we are creating bean .

This is our app class which is dependent on student class, Here we creating object of ApplicationContext, And then creating object of student class using setter injection as we see.

Output :-

Hello World!

Student[studentId=103, studentName=manish singh, studentAdress=jaipur]

Constructor injection :

In this process we provide data to the object using constructor

let’s see with example:

Now we are using constructor to fill the data of object.

To use construction injection we use <conastruct-arg> tag to bean values.

This is our main app. where we are fetching data.

Advantages of Dependency Injection :

→our code is clean and more readable.

→Codes are loosely coupled.

→Reduce unnecessary dependencies.

→Code can be easily testable with different mock implementation.

→ More reusable as the implementations are configured in the XML file, it can be used in a different context.

→Makes Unit testing easy.

→Increased module re-usability.

→Increased system maintainability, because logic changes in the domain affect fewer modules.

Conclusion :

Spring Inversion of Control(IOC) helps in the creation of loosely coupled applications because of Dependency Injection. By implementing Inversion of Control, a software get more controls/options over the software/objects, instead of being controlled or having fewer options.

Hope you guys find it useful !

Thanks

--

--