Explain the concept of dependency injection in the context of Spring MVC.

Dependency injection is the design pattern where an object's dependencies are provided to it by an external container rather than the object creating them itself. In Spring MVC, this means controllers declare what services or repositories they need, and the Spring IoC container supplies those instances automatically.

Key Points

Example

A controller that needs to send emails declares a final EmailService field set through its constructor; Spring provides the real EmailService bean in production, while a test can pass in a mock EmailService instead.

Interview Tip

A concise interview answer is

Dependency injection means Spring's IoC container creates and wires up the objects a class needs instead of the class creating them itself. In practice, I declare dependencies as constructor parameters, and Spring resolves and injects the right beans automatically, which keeps the code loosely coupled and easy to test with mocks.

Last updated: 16 July 2026

Previous question: What configuration steps are needed for method-level security in Spring?

Next question: How does Spring MVC utilize dependency injection with controllers?

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions