Spring MVC Auto-Configuration at Startup — What Spring Boot Does
Fast recall
Quick revision
Core answer
Understanding of Spring Boot auto-configuration for Spring MVC and what happens behind the scenes
Not knowing why adding spring-boot-starter-web is sufficient to get a working REST API — magic is auto-configuration
Mention WebMvcAutoConfiguration class and how adding @EnableWebMvc disables auto-configuration, forcing manual configuration
Interview-ready guide
Interview answer
With spring-boot-starter-web, Boot normally creates the servlet application context, registers DispatcherServlet, and applies MVC defaults. RequestMappingHandlerMapping inspects controller beans after they are registered. If Jackson is available, MVC can write JSON. If Thymeleaf is also installed, its view resolver can render templates. User-defined beans and @EnableWebMvc can replace or change parts of these defaults, so the startup story should always mention conditions and back-off.
Spring MVC Auto-Configuration at Startup — What Spring Boot Does
Direct Answer
When a servlet web application has Spring MVC on the classpath and no conflicting user configuration, Spring Boot auto-configures the DispatcherServlet and MVC infrastructure. Spring itself detects @Controller beans and builds request mappings. Message converters and view resolvers are added conditionally from the libraries and beans present; Jackson or Thymeleaf is not guaranteed unless its support is on the classpath.
Code Example
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
// On startup:
// 1. WebMvcAutoConfiguration registers DispatcherServlet at /
// 2. RequestMappingHandlerMapping scans OrderController
// 3. Jackson HttpMessageConverter registered for JSON
// 4. Embedded Tomcat starts on port 8080
}
}
@RestController
@RequestMapping("/api/orders")
class OrderController {
@GetMapping("/{id}")
public Order getOrder(@PathVariable Long id) {
// Auto-configured Jackson converts Order record to JSON
return new Order(id, "Laptop", 1299.99);
}
}
record Order(Long id, String product, Double price) {}
// No web.xml, no DispatcherServlet config, no ObjectMapper bean
// Spring Boot auto-configuration did it all
Common Mistakes & Pitfalls
- Not understanding why it works: Freshers add spring-boot-starter-web and write a controller — it works, but they don't know DispatcherServlet, HandlerMapping, and Jackson were auto-configured. This bites them when troubleshooting.
- Adding @EnableWebMvc accidentally: This annotation disables Spring Boot's auto-configuration, forcing you to manually configure everything. Freshers copy it from old Spring tutorials and wonder why their app breaks.
- Assuming manual beans override auto-config: If you define your own ObjectMapper bean, Spring Boot's auto-configured one backs off. Freshers don't realize this and create duplicate beans without understanding the conditional logic.
- Not checking auto-configuration report: Running with --debug flag shows which auto-configurations applied — freshers never look at this when debugging configuration issues.
Check your understanding
Answer these without looking back at the article.
- What is WebMvcConfigurer and when would you use it?
- What does @EnableWebMvc do and when should you avoid it?
- How does Spring Boot serve static resources?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.