Spring Boot Is Easy: IOC and DI

Rohit Satwadhar
4 min readSep 24, 2023

--

Spring boot

Inversion of control (IOC) is a key part of spring framework design. IOC is a design principle that helps manage the flow of control in an application and makes it easier to develop and maintain. IOC in simple terms means outsourcing the responsibility of creating and managing objects. In Spring this is done by “Spring Container”.

Dependency injection (DI) is a design pattern which facilitates loose coupling between components in an application and helps manage component dependencies efficiently. DI in simple terms means providing a dependent component with the dependencies it needs.

These two used together can help architect modular, maintainable and testable software.

In this article, we won’t dive deep into these concepts. We will only see how we can use these in our spring applications with annotations.

For the below explanation, we have used some common setups of dependencies and dependent classes. Let’s create these classes now.

We will start with creating an interface and two classes implementing this interface. These classes will act as dependencies.

Coach Interface
Two classes implementing the Coach interface

Now that we have created dependencies components. It’s time to create a dependent component. We will use a Controller. In this controller, we will inject dependencies.

Controller class

There are two variables of Coach interfaces we will inject these. So their respective object can be used in our request mapping methods.

These are three ways you inject dependencies in spring:

  1. Constructor injection:

In the Constructor injection, object dependencies are provided through the constructor of the dependent class. When the dependent class is instantiated Spring container automatically injects the dependencies.

From the above setup, we will inject the coach through constructor injection.

@Autowired annotation tells the spring container to inject dependency of the type Coach. Because there are two implementations of the Coach interface we have to explicitly state which implementation to inject using @ Qualifier and pass it the name of the component we want to inject. When the object of the controller is created Spring container will inject the dependency automatically.

Spring.io team recommends this type of injection when you have required dependencies. This should be your first choice.

2. Setter / Method injection :

In setter injection, dependencies are provided through setter methods after the object has been instantiated.

This allows for more flexibility compared to constructor injection, especially when dealing with optional or changeable dependencies.

Same as above we need to use @Autowired and @ Qualifier annotations.

3. Field Injection :

Most easy to use but not recommended by the spring.io team. This type of injection does not provide null safety, or immutability and it is hard to unit test. Spring official documentation has stopped listing field injection as one of the DI options anymore.

For the sake of an example, we will include this here.

Conclusion:

In conclusion, Spring Boot’s DI and IoC can help you write more robust and adaptable code, making it easier to respond to changing requirements and deliver high-quality software.

Thank you for exploring the nuances of injection types in Spring Boot with me. Happy coding!

And Remember Spring Boot Is Easy.

--

--

Rohit Satwadhar

I Write about new things that I learn. That is how I remember stuff. These things are mostly tech related.