Understanding Inversion of Control and Dependency Injection

DZone 's Guide to

Understanding Inversion of Control and Dependency Injection

In this article, learn to understand inversion of control and dependency injection.

· Java Zone ·
Free Resource

There are two key concepts that you must understand when you start with Spring or JakartaEE/CDI–inversion of control and dependency injection.

In brief, inversion of control means letting a framework take control of the execution flow of your program to do things like create instances of your classes and inject the required dependencies.

Suppose you have to code a Java application that calculates the age of a person using their birth date. You can start by coding the logic that calculates the age as follows:


You can also code a web UI that allows users to enter a date and then click a button to calculate the corresponding age. Here's an implementation using Vaadin:


Notice how we instantiate AgeService directly in the MainView class. Although this is not necessarily a bad thing, we can improve the code. Especially, if we use a framework such as Spring or CDI (JakartaEE).

You may also like: Spring Framework Basics: What Is Inversion of Control?

An evident problem with the previous code is that if we wanted to write a unit test for the calculateAge method, we would end up testing code in theAgeService class as well. This is something you should avoid in unit testing. If the test fails, we want to be completely sure that the code failed in the calculateAge method, not in the AgeService class.

In unit testing, you create mocks of the classes you don't want to test and need a way to pass the mock to the tested class. In the previous example, this is not possible since ageService is instantiated in private code. To solve this, we can let clients of MainView pass the instance of AgeService in the constructor as follows:


Now, a unit test can pass a mock of AgeService. But what happens with the actual application? Which instance is passed?

In this example application, Vaadin creates instances of MainView, when needed. It does this through either Spring or CDI when they are used. This means that the instances are managed by these frameworks, and when you run your application, you simultaneously yield control of your application to the Spring or CDI framework. The framework then controls the flow of the application and calls your code. In the previous example, it calls the constructor of the MainView class. This is what is called inversion of control. The framework is also allowed to do other things. For example, it can pass dependencies declared in the constructor. This process is called dependency injection.

If you use Spring, you need to make sure it manages the instances of AgeService. You can do this by using the @Component or @Service annotations. For example:


If you use CDI, you don't have to add anything to the AgeService class, but you need to mark the constructor of MainView with the @Inject annotation.

Here's a video that explains in more detail the concepts covered in this article:

You can find the source code on GitHub.

Further Reading

Inversion of Control (Explained Non-Technically)

Understanding Inversion of Control [Snippet]

dependency injection ,inversion of control ,jakarta ee ,java ,patterns ,software design ,software patterns ,spring ,vaadin ,web development

Published at DZone with permission of Alejandro Duarte . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}