At its core, Spring is a DI framework. Before we get started with understanding Spring, we need to understand the concepts of dependency and DI.
Object-oriented applications are built around objects and their interaction with other objects. Typical applications involve thousands of objects interacting with each other.
Let's consider an example class, BusinessServiceImpl, as follows:
public class BusinessServiceImpl {
public long calculateSum(User user) {
DataServiceImpl dataService = new DataServiceImpl();
long sum = 0;
for (Data data : dataService.retrieveData(user)) {
sum += data.getValue();
}
return sum;
}
}
BusinessServiceImpl creates an instance of DataServiceImpl and uses it to get the data from the database. DataServiceImpl is a dependency of BusinessServiceImpl.
Dependencies of a class are the other classes it depends on to fulfill its responsibilities.