What is Dependency
We say Class A has dependency on Class B if Class A is using Class B reference as Variable.
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