Thursday, August 20, 2015

Spring - What exactly Dependency Injection is ?

Dependency Injection (DI):

The whole  Spring framework roam around the concept of  Dependency Injection (DI)  which is an implementation of  Inversion of Control. The Inversion of Control (IoC) is a software architect, which goes against the traditional procedural programming.

From wiki  of procedural programming is identified as 

In traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or task-specific, code.

When writing a complex Java application, traditional programming makes an application tightly coupled between the connection points and make the code almost disqualifies to reuse. And then the term Inversion of Control coined.

With Inversion of Control (IoC) a drastic change happened in software architect and complex applications became a bunch of modules. With IoC, modularity of the application increases and thus extensibility as well. 

Coming to Dependency Injection -What is dependency injection exactly?  Dependency Injection is a methodology which implements IoC. This term really became a jargon and puzzling many minds for years. The simplest definition I found for the term DI is by James Shore 

Dependency injection means giving an object its instance variables. Really. That's it. 

Imagine Class Y is dependent on Class X. When ever there is a need of  Class X for Class Y, X will be injected to Y. That is all. There are several ways of Injections like Constructor Injunction, Setter Injection, Interface Injection. All these type are injection do the same but the ways are different. Main goal is to inject the dependency to it's receiver. 

No comments:

Post a Comment