Difference Between IoC and Dependency Injection in Spring



Inversion of control is a design principle which helps to invert the control of object creation.

According to the paper written by Martin Fowler , inversion of control is the principle where the control flow of a program is inverted: instead of the programmer controlling the flow of a program, the external sources (framework, services, other components) take control of it. It's like we plug something into something else. He mentioned an example about EJB 2.0.

Dependency Injection is a design pattern which implements IOC principle. DI provides objects that an object needs. Let’s say, class X is dependent on Y. So rather than creating object of Y within the class “X”, we can inject the dependencies via a constructor or setter injection.

Sr. No. Key IOC Dependency Injection
1
Design Principle
It is design principle where the control flow of the program is inverted
It is one of the subtypes of the IOC principle  
2
Implementation
It is a term which is implemented by multiple design patterns  service locator , events , delegates and dependency Injection
DI is design pattern which can be achieved by constructor and setter injection
3
Use Case
Aspect oriented programing is one way to implement IOC  
 In Case of change in business requirement no code change required
Updated on: 2020-09-09T08:50:55+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements