Spring Framework Basics: What Is Inversion of Control?
What is Inversion of Control? And how does the Spring Framework implement it?
Join the DZone community and get the full member experience.
Join For FreeDevelopers starting with the Spring Framework often get confused with the terminology, specifically dependencies, dependency injection, and Inversion of Control. In this article, we introduce you to the concept of Inversion of Control.
What You Will Learn
- What is Inversion of Control?
- What are some examples of Inversion of Control?
- How does the Spring Framework implement Inversion of Control?
- Why is Inversion of Control important and what are its advantages?
What Is Inversion of Control?
Approach-1
Have a look at the following implementation of ComplexAlgorithmImpl
:
public class ComplexAlgorithmImpl {
BubbleSortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm();
//...
}
One of the numerous things that ComplexAlgorithmImpl
does is sorting. It creates an instance of BubbleSortAlgorithm
directly within its code.
Approach-2
Now, look at this implementation for a change:
public interface SortAlgorithm {
public int[] sort(int[] numbers);
}
@Component
public class ComplexAlgorithmImpl {
@AutoWired
private SortAlgorithm sortAlgorithm;
//...
}
ComplexAlgorithmImpl
here makes use of the SortAlgorithm
interface. It also provides a constructor or a setter method where you can set the SortAlgorithm
instance into it. The user tells ComplexAlgorithmImpl
, which sort algorithm to make use of.
Comparing Approach-1 and Approach-2
Approach-1
ComplexAlgorithmImpl
can only useBubbleSortAlgorithm;
it is tightly coupled.- If we need to change
ComplexAlgorithmImpl
to use quicksort, the relevant code needs to be changed entirely. - The control over the
BubbleSortAlgorithm
dependency is with theComplexAlgorithmImpl
class.
Approach-2
ComplexAlgorithmImpl
is open to using any implementation ofSortAlgorithm
, it is loosely coupled.- We only need to change the parameter we pass to the constructor or setter of
ComplexAlgorithmImpl
. - The control over the
SortAlgorithm
dependency is with the user ofComplexAlgorithmImpl
.
Inversion Of Control At Play!
In Approach-1, ComplexAlgorithmImpl
is tied to a specific sort algorithm.
In Approach-2, it says: give me any sort algorithm and I will work with it.
This is Inversion of Control.
Instead of creating its own dependencies, a class declares its dependencies. The control now shifts from the class to the user of the class to provide the dependency.
Why Is Inversion of Control Important?
Once you write code with Inversion of Control, you can use frameworks like Spring to complete dependency injection and wire up beans and dependencies.
Advantages of Inversion Of Control
- Inversion of Control makes your code loosely coupled
- Inversion of Control also makes it easy for the programmer to write effective unit tests
Lastly, be sure to check out the video below on IoC:
Summary
In this article, we talked about Inversion of Control. Instead of a class creating an instance of its own dependency, it leaves it to the user of the class to pass it in and makes code loosely coupled.
Hope you learned something! Let us know what you think in comments below.
Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments