Close

Understanding Spring Boot Web MVC Auto Configuration

[Last Updated: Nov 30, 2026]

WebMvcAutoConfiguration is the class that sets up Spring Web MVC for us automatically. Below is a simplified snippet of this class (Spring Boot 4.0):

package org.springframework.boot.webmvc.autoconfigure;
 .......
@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class,
        TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
public class WebMvcAutoConfiguration {
 ...
}

From the snippet above, these conditions must be true for this auto configuration to work:

  1. Servlet, DispatcherServlet and WebMvcConfigurer classes must be on the classpath. If we add spring-boot-starter-webmvc to the project, all of these classes are available.
  2. The application must be a servlet based web application (@ConditionalOnWebApplication(type = Type.SERVLET)). When we start the application with SpringApplication, Spring Boot checks the classpath and decides the application type. For a servlet web application it creates AnnotationConfigServletWebServerApplicationContext. If only Spring WebFlux is present, it creates a reactive web application. If there is no web library at all, it creates a plain AnnotationConfigApplicationContext. We can also set the type ourselves with the property spring.main.web-application-type.
  3. WebMvcConfigurationSupport must not already be registered as a bean. In a plain Spring MVC application, this class is registered when we use @EnableWebMvc. So if we add @EnableWebMvc to a Spring Boot application, this auto configuration backs off, and we lose the Boot defaults.

The other two annotations control the order. @AutoConfigureOrder gives this class a very high priority, and after makes sure it runs after the DispatcherServlet, task execution and validation auto configurations.

Adding the Dependency

With Maven, add spring-boot-starter-webmvc to the pom.xml. With Gradle, add the same starter with implementation in build.gradle or build.gradle.kts.

Adding additional Spring Web MVC Components

If we need more MVC configuration (interceptors, formatters, view controllers etc.), we can add our own @Configuration class that implements WebMvcConfigurer (example here). We must not add @EnableWebMvc to this class, otherwise Boot's MVC auto configuration is turned off. Here is a small example:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
    }
}

Where to find Boot MVC Configurations?

It is good to know where Spring Boot registers and configures MVC beans. Then we can investigate and find answers ourselves when they are missing from the documentation. Here are two important nested classes of WebMvcAutoConfiguration:

WebMvcAutoConfigurationAdapter implements WebMvcConfigurer and registers the beans that Boot needs for Web MVC, such as view resolvers and message converters.

The other class is EnableWebMvcConfiguration. It extends DelegatingWebMvcConfiguration, which finds all WebMvcConfigurer beans in the application context and applies them. This is how our own configuration (as shown in the last section) gets picked up.

Version History

The final tutorial shows the latest code. Here is how things were different in older versions:

  • Spring Boot 1.x: The auto configuration was annotated with @Configuration and was listed in META-INF/spring.factories. We had to extend the abstract class WebMvcConfigurerAdapter to add our own MVC configuration. The web context class was AnnotationConfigEmbeddedWebApplicationContext.
  • Spring Boot 2.0 (Spring Framework 5): WebMvcConfigurer got default methods (Java 8), so the adapter class was not needed anymore and was deprecated. The context was renamed to AnnotationConfigServletWebServerApplicationContext. Because Spring WebFlux was added, the condition became @ConditionalOnWebApplication(type = Type.SERVLET).
  • Spring Boot 2.7: The new @AutoConfiguration annotation was added. Auto configurations could now be listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
  • Spring Boot 3.0 (Spring Framework 6): Java 17 became the minimum version, and javax.servlet changed to jakarta.servlet. Removed: WebMvcConfigurerAdapter no longer exists, so we must implement WebMvcConfigurer directly. Registering auto configurations in spring.factories is no longer supported.
  • Spring Boot 4.0 (Spring Framework 7): Spring Boot is now split into smaller modules. WebMvcAutoConfiguration moved to the spring-boot-webmvc module and the package org.springframework.boot.webmvc.autoconfigure. The starter spring-boot-starter-web was renamed to spring-boot-starter-webmvc. The baseline is now Servlet 6.1 (Jakarta EE 11).

Deprecations and Removals

  • WebMvcConfigurerAdapter is removed (since Spring Framework 6.0). Use WebMvcConfigurer.
  • AnnotationConfigEmbeddedWebApplicationContext is gone (renamed in Spring Boot 2.0).
  • Registering auto configurations through spring.factories is removed (Spring Boot 3.0).
  • The old starter name spring-boot-starter-web is replaced by spring-boot-starter-webmvc in Spring Boot 4.0.

See Also

Join