What is dependency injection in Spring and how does the IoC container work?
Learn what dependency injection is in Spring, how the IoC container wires beans, and why constructor injection is preferred — with examples and interview tips.
Expected Interview Answer
Dependency injection (DI) is a pattern where an object receives its dependencies from an external source rather than creating them itself, and in Spring the IoC (Inversion of Control) container supplies those dependencies at runtime.
The IoC container reads configuration (annotations, Java config, or XML), instantiates the beans it manages, resolves their dependencies, and wires them together — inverting control of object creation from your code to the framework. Spring supports constructor, setter, and field injection; constructor injection is preferred because it makes dependencies explicit, supports immutability, and enables reliable testing. The two core container interfaces are BeanFactory and the richer ApplicationContext.
- Loose coupling between components
- Easier unit testing via mock injection
- Centralized configuration and lifecycle management
- Promotes coding to interfaces, not implementations
- Simplifies swapping implementations without code changes
AI Mentor Explanation
A batter never sources their own bat, pads, and gloves mid-innings; the team kit manager hands over exactly the right gear before they walk out. The IoC container is that kit manager: the batter (bean) just declares what they need and receives it ready to use, so the player focuses on scoring, not on procuring equipment.
Step-by-Step Explanation
Step 1
Define beans
Annotate classes with @Component/@Service/@Repository or declare them via @Bean methods in a @Configuration class.
Step 2
Declare dependencies
Express what a class needs through constructor parameters (preferred), setters, or fields marked @Autowired.
Step 3
Container startup
The ApplicationContext scans configuration, builds a bean definition registry, and instantiates the managed beans.
Step 4
Resolve and inject
The container resolves each dependency by type (and qualifier if needed) and wires it into the target bean.
Step 5
Manage lifecycle
The container handles scopes, init/destroy callbacks, and hands ready-to-use beans to your application.
What Interviewer Expects
- Clear definition of DI and Inversion of Control
- Difference between BeanFactory and ApplicationContext
- The three injection types and why constructor injection is preferred
- How the container resolves dependencies by type and qualifier
- Awareness of testability and loose-coupling benefits
Common Mistakes
- Confusing dependency injection with the service locator pattern
- Claiming field injection is best practice
- Not knowing ApplicationContext extends BeanFactory
- Overusing @Autowired on fields, hurting testability
- Failing to explain what 'inversion of control' actually inverts
Best Answer (HR Friendly)
“Dependency injection means Spring hands each object the other objects it needs instead of the object building them itself. A container called the IoC container creates and connects these pieces automatically, which keeps the code loosely coupled and easy to test.”
Code Example
@Service
public class OrderService {
private final PaymentGateway gateway;
// The container injects PaymentGateway automatically
public OrderService(PaymentGateway gateway) {
this.gateway = gateway;
}
public void checkout(Order order) {
gateway.charge(order.total());
}
}Follow-up Questions
- What is the difference between BeanFactory and ApplicationContext?
- Why is constructor injection preferred over field injection?
- How does Spring resolve ambiguity when multiple beans match a type?
- What are the different bean scopes in Spring?
- How would you inject a dependency that is only known at runtime?
MCQ Practice
1. What does the 'Inversion of Control' in Spring's IoC container refer to?
IoC means control over instantiating and wiring dependencies is inverted from your code to the container.
2. Which injection type does Spring generally recommend?
Constructor injection makes dependencies explicit and mandatory, supports immutability, and improves testability.
3. Which interface is the richer, most commonly used Spring container?
ApplicationContext extends BeanFactory and adds features like events, i18n, and AOP integration.
Flash Cards
What is dependency injection? — A pattern where an object receives its dependencies from an external source instead of creating them itself.
What does the IoC container do? — It instantiates beans, resolves their dependencies, wires them together, and manages their lifecycle.
Preferred injection type? — Constructor injection — explicit, immutable, and test-friendly.
BeanFactory vs ApplicationContext? — ApplicationContext extends BeanFactory with events, i18n, resource loading, and AOP support.
Continue Learning
Related Interview Questions
What is a Spring profile and how do you configure environment-specific beans?
medium
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is Spring AOP and how do aspects, advice, and pointcuts work?
medium
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy