Difference between IOC and Dependency Injection in Spring.

SpringObject Oriented ProgrammingProgramming

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.KeyIOCDependency 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
raja
Published on 09-Sep-2020 12:20:18
Advertisements