Dependency Injection in Spring Explained
Fast recall
Quick revision
Core answer
new UserRepository() inside a service, the class declares what it needs and the Spring IoC container injects a ready-to-use instance through the constructor. This separation of object creation from usage is what makes Spring applications loosely coupled and easy to test.Whether you understand that DI is how Spring implements Inversion of Control, and why handing dependencies to the container makes code testable and loosely coupled.
Confusing DI with IoC and treating them as the same thing — IoC is the broad principle, DI is the specific technique Spring uses to apply it.
Show a concrete payoff: a constructor-injected repository can be replaced with a mock in a unit test, so the test needs no real database.
Interview-ready guide
Interview answer
Dependency Injection is a design pattern where Spring gives an object its dependencies instead of the object creating them.
So if my OrderService needs an OrderRepository, I don't write new OrderRepository inside the service. I just declare the repository in the constructor, and Spring injects a ready-to-use instance. This is how Spring implements Inversion of Control, which means the framework controls object creation, not my code.
The biggest advantage is loose coupling. My OrderService depends on an interface, not a concrete class, so I can swap the database implementation for an in-memory one without touching the service. It also makes testing much easier.
In a unit test I can inject a mock repository and check the order logic with no real database connection.
A common mistake is using field injection with the Autowired annotation on a private field because it looks simpler. Constructor injection is better because the dependencies are explicit in the signature and you can mark the fields final.
When an interviewer asks this, they want to know that you understand why DI exists, not just what it is, so I always mention testability and loose coupling with a concrete example like swapping a repository implementation.
Dependency Injection in Spring Explained
Direct Answer
Dependency Injection (DI) is a design pattern where Spring supplies an object with its dependencies instead of the object creating them itself. Instead of writing new UserRepository() inside a service, the class declares what it needs and the Spring IoC container injects a ready-to-use instance through the constructor. This separation of object creation from usage is what makes Spring applications loosely coupled and easy to test.
Code Example
@Service
public class OrderService {
private final OrderRepository repository;
private final NotificationService notifier;
// Spring injects both dependencies through the constructor
public OrderService(OrderRepository repository,
NotificationService notifier) {
this.repository = repository;
this.notifier = notifier;
}
public Order createOrder(OrderRequest request) {
var order = new Order(request.customerId(), request.items());
repository.save(order); // injected dependency, no 'new'
notifier.sendConfirmation(order); // injected dependency
return order;
}
}
@Repository
public class OrderRepository {
public void save(Order order) {
// database persistence logic
}
}
The final fields make each dependency mandatory and immutable once the bean is built.
Common Mistakes & Pitfalls
- Using
newfor Spring-managed beans: writingnew UserRepository()inside a service bypasses DI entirely, creating a plain object with no transaction management or connection wiring. - Confusing DI with IoC: IoC is the principle (the framework controls the flow), DI is the technique (the framework injects dependencies). They're related, not identical.
- Field injection in production code:
@Autowiredon a private field looks simpler, but you can't pass a mock through a constructor, so testing gets harder. Constructor injection is the recommended approach because it makes dependencies explicit and supportsfinalfields.
Check your understanding
Answer these without looking back at the article.
- What are the three types of dependency injection?
- What is the difference between IoC and DI?
- Why is constructor injection preferred over field injection?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.