Dependency Injection in Spring:-
Dependency Injection (DI) is a design pattern that removes the dependency
from the programming code so that it can be easy to manage and test the
application. Dependency Injection makes our programming code loosely coupled.
Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern.
The Dependency Injection is a design pattern that removes the
dependency of the programs. In such case we provide the information from the
external source such as XML file. It makes our code loosely coupled and easier
for testing. In such case we write the code as:
class Employee{
Address add;
Employee(Address add){
this.add=add;
}
public void setAddress(Address add){
this.add=add;
} }
In this case, instance of Address class is provided by external souce such as XML file either by constructor or setter method.