Dependency Injection & IoC


Inversion of Control

  • Inversion of Control is a design pattern in which the flow of control of a system is inverted in comparison to procedural/traditional programming.
  • It can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways rather than the internal program controlling the flow.
  • It could be used for decoupling components and layers in the system. 
  • The pattern is implemented through injecting dependencies into a component when it is constructed.



Dependency Injection

  • Dependency injection is a style of object configuration in which an objects fields and collaborators are set by an external entity.
  • The traditional method utilizes 'Dependency Lookup' to resolve its dependencies.
  • It is a process injecting (or initializing, resolving and pushing) dependencies to an object, rather than letting it to resolve by itself.

In Spring, the IoC is implemented by Spring IoC Container.

Advantages:

  1. Your code is clean and more readable.
  2. Codes are loosely coupled.
  3. More reusable as the implementations are configured in the XML file, it can be used in a different context.
  4. Code can be easily testable with different mock implementation.

Comments