Spring Dependency Injection

What is Dependency

We say Class A has dependency on Class B if Class A is using Class B reference as Variable.
Example:

What is Dependency Injection

Classes in Java are suppose to independent to each other(As much as possible). Less dependency increases the possibility to reuse classes and to test them independently of other classes. Dependency Injection can be used to achieve this. Instead of class search for it’s dependency, In Dependency Injection Container will identify the Dependency and inject those dependency in Class dynamically.

Dependency Injection in Spring

Spring uses the Dependency Injection design pattern to define the Object dependency. Spring provide two type of dependency injection

  • Constructor Injection
  • Setter Injection

 

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.