Inversion of Control (IoC), also called as Dependency Injection (DI) is a process where Objects defines their dependencies, meaning that the other objects that it works with, defined through Constructor arguments, Factory method arguments or the properties set on the Object. This process allows an Object to decouple direct dependency other Objects at the time of instantiation or creation.

Contents

Let's understand this with an example, say we are creating an application called , with features like and and they also use other internal features like for example SpellChecker may use a dictionary etc, and this is a overhead if you imagine for a large scale application.

If you visualize traditional way of implementing Notepad application, where Notepad has a direct dependencies on SpellChecker and LettersCounter and these objects are tightly coupled, this is how it looks.

Objects that are tightly coupled

As you observe in below code, when the Notepad is instantiated, it automatically instantiating its dependencies as well.

public class Notepad { private SpellChecker spellChecker; private LettersCounter lettersCounter; public Notepad() { this.spellChecker = new SpellChecker(); this.lettersCounter = new LettersCounter(); } }
Inversion of Control (IoC)

Inversion of Control (IoC) removes this direct dependency on Object by introducing an abstraction between an Object and its dependencies. lets's understand this with the same example discussed above.

Below diagram shows Inversion of Control way of implementing this, where Notepad do not access SpellChecker or LettersCounter directly and these objects are served by an abstraction layer DependencyInjector. This process allows Objects to be loosely coupled.

Objects that are tightly coupled

As you observe in below code in the constructor, we are passing SpellChecker and LettersCounter as references that are constructed outside of Notepad.

public class Notepad { private SpellChecker spellChecker; private LettersCounter lettersCounter; public Notepad(SpellChecker spellChecker, LettersCounter lettersCounter) { this.spellChecker = spellChecker; this.lettersCounter = lettersCounter; } }

This process also has below advantages.

Spring Framework

Spring Framework implemented with the Inversion of Control at the core and it created a component called Spring IoC Container, on top of adopting IoC pattern, it also added other features like Aspect Oriented Programming (AOP) and Injecting Objects via Configuration (XML, YAML and Annotation based) etc.

In Spring Framework, there are mainly two components and , Beans are nothing but the business objects that the application works with, these are simple Java POJO's, and configuration is the meta data that defines how these Beans should behave.

Spring's IoC container acts as an Engine that is responsible creating, configuring, assembling and managing the beans using the meta data supplied.

Spring IoC Container

Above diagram shows how the Spring IoC takes Beans and application configuration as inputs and serves the application as ready to use components.