why spring-boot application doesn't require EnableWebMvc
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a popular framework that simplifies the process of building stand-alone, production-grade Spring applications. One of the notable conveniences it offers is the configured setup for web applications, notably the ability to create Spring MVC applications without the need for the `@EnableWebMvc` annotation. In this article, we'll delve into why Spring Boot applications don't require this annotation and explore the underlying mechanics that support this behavior.
Understanding Spring Boot and Spring MVC
Before diving into the specifics, it's essential to understand the basics of how Spring Boot and Spring MVC work together:
- Spring Boot: An opinionated framework that simplifies Spring applications’ configuration. It achieves this using auto-configuration, which can automatically set up Spring context based on JAR dependencies in the classpath.
- Spring MVC: A module providing Model-View-Controller architecture components for web applications. In traditional Spring applications, users would configure Spring MVC manually.
- `@EnableWebMvc`: In traditional Spring projects, this annotation signals that the application should act as a web application, allowing the registration of certain components like view resolvers, handler mappings, etc.
Spring Boot’s Auto-Configuration
A cornerstone of Spring Boot's design is auto-configuration. This mechanism automatically configures your application based on the libraries present on the classpath. When it comes to Spring MVC:
- Web Dependency: When the `spring-boot-starter-web` dependency is included, Spring Boot determines that the application should function as a web application.
- MVC Auto-Configuration: Spring Boot automatically configures essential components of Spring MVC so that developers don't have to provide manual configurations, as would be typically required with `@EnableWebMvc`.
Key Components Configured Automatically
- DispatcherServlet: The central component for handling web requests, it's registered automatically.
- ViewResolvers: Handles views (e.g., Thymeleaf, JSP) and forwards them correctly.
- MessageConverters: Automatically configured converters allow handling JSON/XML data transfer effortlessly using libraries like Jackson or Gson.
Why `@EnableWebMvc` Is Unnecessary
- When you add `@EnableWebMvc` to a configuration class, Spring Boot knows you want full control over Spring MVC configuration and opts out of automatic configuration. Therefore, traditional configuration becomes manual and explicit.
- Spring Boot's Primary Goal is to remove the need for defining boilerplate configurations. By not using `@EnableWebMvc`, you surrender control to Spring Boot’s auto-configuration and benefit from immediate workable configurations suitable for most use cases.
- Extend `WebMvcConfigurer`: Allows additional MVC configurations while retaining the auto-configuration.
- Properties Configuration: Some configurations can be adjusted by modifying properties in `application.properties` or `application.yml`.
Related reading
- Why Spring Boot 2.0 application does not run schema.sql?
- Why Spring Boot Application class needs to have Configuration annotation?
- Why spring boot generates jar or war file with .original extension?
- Why there is no ConcurrentHashSet against ConcurrentHashMap
- Why to use AllArgsConstructor and NoArgsConstructor together over an Entity?
- Why use a prime number in hashCode?
- Why use a ReentrantLock if one can use synchronized(this)?
- Why use a ReentrantLock if one can use synchronizedthis?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.