Spring MVC controllers rely on Spring's IoC container to supply their dependencies — services, repositories, and other components — rather than constructing them manually. This is the same dependency injection mechanism used throughout the framework, applied to the web layer.
A controller that needs to look up products declares a final ProductService field set through its constructor; Spring automatically supplies the real ProductService bean when the application starts, and a test can supply a Mockito mock instead.
@RestController
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping("/products/{id}")
public Product getProduct(@PathVariable Long id) {
return productService.findById(id);
}
}A concise interview answer is
Spring MVC controllers get their dependencies through the same IoC container as everything else — I prefer constructor injection so dependencies are explicit and the class can be tested with mocks. Spring resolves the beans by type at startup, so the controller never has to instantiate its services itself.
Last updated: 16 July 2026
Previous question: Explain the concept of dependency injection in the context of Spring MVC.
Next question: What types of dependency injection does Spring MVC support?