Spring Dependency Injection

Here we learn what is Dependency Injection and how Spring frame work supports Dependency Injection.

DI is a Jargon created by by Martin Fowler and it is also called as inversion of control. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing).In object oriented design, objects have relationship with one another.
A class (A) has a dependency to class (B) if class uses class (B) as a variable.
If dependency injection is used then the class (B) is given to class (A) via
  • the constructor of the class (A) - this is then called construction injection
  • a setter - this is then called setter injection

A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a DAO (Data Access object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.
 A software design based on dependency injection is possible with standard Java.
Process diagram of  DI

0 comments:

Post a Comment