before we talk about IOC (Inversion of control) and DI (dependency injection) i will firstly explain why it is used.
so basically when we create different classes in a spring boot application excluding the main class, to call them in the main class we have to create an object. this works if you work in a small project.
but if you are working in big organizations you have to manage tons of classes. and manually creating an object of each particular class makes it messy. so here is where IOC and DI work to solve this problem.
Ioc :- ioc is a design principle where a framework or container takes over program flow of dependency management.
in simple words as i mentioned above, if we create tons of classes it gets messy. so Ioc is a principle where we handover object creation to the container. and ioc takes a separate space in heap memory to manage all of object creation and it wont affect the program.
DI :- Dependency injection is the implementation of the Ioc principle. in spring boot we use the @Component annotation over the subclasses, and we use the @Autowired annotation to actually inject and use them.
in this way we can create multiple classes without worrying about managing them. Dependency injection in spring is able to manage all of that.
Dependency injection is one of the necessary features in spring boot, it saves your time as well as creates a simple workflow.
Top comments (0)