IOC and Dependency Injection

Spring is most known with is the Dependency Injection (DI) flavor of Inversion of Control(IOC).

What is IOC and Dependency Injection?

Inversion of control (IoC) is a programming technique in which object coupling is bound at run time by an assembler object and is typically not known at compile time.

What is Dependency Injection?
Dependency Injection is a way to achieve Inversion of control (IoC) in our application by moving objects binding from compile time to runtime. We can achieve IoC through Factory Pattern, Template Method Design Pattern, Strategy Pattern and Service Locator pattern too.

Spring, Google Guice and Java EE CDI frameworks facilitate the process of dependency injection through use of Java Reflection API and java annotations. All we need is to annotate the field, constructor or setter method and configure them in configuration xml files or classes.

Differentiating with dependency injection
Inversion of control is a design paradigm with the goal of giving more control to the targeted components of your application, the ones getting the work done.
Dependency injection is a pattern used to create instances of objects that other objects rely on without knowing at compile time which class will be used to provide that functionality. Inversion of control relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality.
The two concepts work together in this way to allow for much more flexible, reusable, and encapsulated code to be written. As such, they are important concepts in designing object-oriented solutions.
Implementing inversion of control design pattern
In object-oriented programming, there are several basic techniques to implement inversion of control. These are:

  • using a factory pattern
  • using a service locator pattern
  • using a dependency injection

There are 3 types of dependency Injection.

  • Constructor Injection : Dependencies are provided as constructor parameters.
  • Setter Injection : Dependencies are assigned through JavaBeans properties (ex: setter methods).
  • Interface Injection: Injection is done through an interface.
   

Leave a Reply

Your email address will not be published.