Here's a detailed explanation of dependency and how it works in the Spring Framework:
What is Dependency?
In software development, a dependency refers to a component (e.g., class, module, or library) that relies on another component to function properly. In other words, a dependency is a relationship where one component (dependent) needs another component (dependency) to:
Perform its tasks
Provide a required service
Use its functionality
Dependencies in Object-Oriented Programming (OOP)
In OOP, dependencies often manifest as:
One class using another class's methods or properties
One class inheriting behavior from another class (inheritance)
One class containing an instance of another class (composition)
Dependency in the Context of Spring Framework
In the Spring Framework, a dependency is a bean (a managed object) that is required by another bean to perform its duties. Spring's primary goal is to manage these dependencies for you, making your application more:
Loosely Coupled: Reducing direct dependencies between beans
Testable: Easier to write unit tests by mocking dependencies
Maintainable: Simplifying the addition, removal, or replacement of dependencies
How Dependency Works in Spring Framework
Here's a step-by-step overview of how dependency works in Spring:
Bean Definition: You define beans in your Spring application using:
XML configuration files (legacy)
Java-based configuration classes (@Configuration)
Annotations on bean classes (@Component, @Service, etc.)
Dependency Declaration: Within a bean, you declare its dependencies using:
Constructor arguments
Setter methods
Field injections (using @Autowired or @Inject)
Dependency Resolution: When the Spring ApplicationContext is created, it:
Scans for bean definitions
Creates and configures beans
Resolves dependencies by:
Identifying matching beans (by type, name, or qualifier)
Injecting the dependencies into the dependent bean
Bean Wiring: Spring "wires" the dependencies together, creating a graph of interconnected beans. This process is also known as Inversion of Control (IoC).
Bean Lifecycle: The ApplicationContext manages the lifecycle of all beans, including:
Creation: Beans are instantiated based on their definitions.
Configuration (dependency injection): Dependencies are injected into the beans.
Usage: Beans are used by the application to perform tasks.
Destruction (when the context is closed): Spring calls destroy methods (if defined) to clean up resources when the application shuts down.
Example (using Java-based configuration and annotations)
java
// Dependency (Bean)
@Component
public class GreetingService {
public String getGreeting() {
return "Hello!";
}
}
// Dependent Bean
@Service
public class MyService {
private final GreetingService greetingService;
// Constructor-based dependency injection
@Autowired
public MyService(GreetingService greetingService) {
this.greetingService = greetingService;
}
public void doSomething() {
System.out.println(greetingService.getGreeting());
}
}
In this example:
GreetingService is a dependency (bean)
MyService is a dependent bean that requires GreetingService
Spring resolves the dependency by injecting GreetingService into MyService through the constructor.