Extend and modify the Spring Boot Web MVC configuration

Section 8.1.1. The “Spring Web MVC Framework” describes how to extend and completely replace Spring Boot’s automatic configuration of Spring MVC

Extend the Web MVC configuration

Spring Boot will automatically configure many components by looking for user-defined configurations or components in the container. Classes annotated with @Bean or @Component annotations will enable automatic configuration if there is no custom Component. It is also possible to combine user-defined components with auto-configured components.

When the SSM framework is used to configure Spring MVC, the configuration is usually carried out in the Spring MVC configuration file. If you want to configure the view mapping, you can use the VIEW-Controller tag of the MVC namespace in the configuration file to configure the interceptor.

There is no Spring MVC configuration file in Spring Boot. How do I do this configuration?

The official website offers the following answers:

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

Write a Configuration class LilithMvcConfig that uses the @Configuration annotation and implements the WebMvcConfigurer interface. Note that the @enableWebMVC annotation is not allowed

@Configuration
public class LilithMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // The browser sends /lilith to the SUCCESS page
        registry.addViewController("/lilith").setViewName("success"); }}Copy the code

Restart the application, the browser enter http://localhost:8080/lilith

The browser outputs the content and successfully implements the view mapping, which was originally implemented in the Spring MVC configuration file.

Why is this feature implemented?

First, WebMvcAutoConfiguration is a Spring Boot class for Spring MVC automatic configuration. WebMvcAutoConfiguration automatic configuration includes a static class WebMvcAutoConfigurationAdapter

WebMvcAutoConfigurationAdapter is realized WebMvcConfigurer, and configure the views mentioned above in the parser and static resource access control, etc

WebMvcAutoConfigurationAdapter using the @ Import annotations into EnableWebMvcConfiguration class, EnableWebMvcConfiguration inherited DelegatingWebMvcConfiguration DelegatingWebMvcConfiguration class includes a setConfigurers method

In the setConfigurers method, the parameters are retrieved from the container. That is, all WebMvcConfigure assignments are retrieved from the container into the Configurers. DelegatingWebMvcConfiguration class all of the following configuration, such as adding format converter

Adding a converter adds all the Formatters in the container to the add all configuration

So custom HttpMessageConverter and ViewResolver will also work because they will also be iterated over, added to the configuration along with the configuration Spring Boot itself has already done

Is that why adding @Configuration annotations and implementing the WebMvcConfigure class allows both custom Configuration and automatic Configuration of Spring MVC in Spring Boot

Completely replace automatic configuration for Web MVC

The website also mentions that if you want full control of Spring MVC, you can add the @enableWebMVC annotation to your custom configuration class

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

Add @enableWebMVC to the LilithMvcConfig config class, restart the application, and access the index1.html page in your browser

Annotate @enableWebMVC, start the application again, and access the index1.html page in your browser

The index1.html page can be accessed normally. The index1.html page cannot be accessed with the @enableWebMVC annotation.

@enableWebMVC invalidates Spring MVC automatic configuration, invalidates all static resource mapping rules, and invalidates all static resources.

Why does @enableWebMV cause all automatic configurations to fail

@ EnableWebMvc import a DelegatringWebMvcConfiguration class

DelegatringWebMvcConfiguration inherited WebMvcConfigurationSupport again

Come back to see WebMvcAutoConfiguration automatic configuration class conditions on the annotation @ ConditionalOnMissingBean (WebMvcConfigurationSupport. Class) that is to say, Only in the WebMvcConfigurationSupport not in container will automatically configure the import containers. Import @ EnableWebMvc annotations to the water in the container a subclass of WebMvcConfigurationSupport DelegatringWebMvcConfiguration, This will result in WebMvcAutoConfiguration not being imported into the container and of course not being able to perform automatic configuration

And @ EnableWebMvc annotations import WebMvcConfigurationSupport subclass DelegatringWebMvcConfiguration contains only the most basic function of Spring MVC; The view resolver, the viewController, the interceptor all need to be configured.