As anyone who has used Spring Boot knows, we just need to introduce the spring-boot-starter-Web dependency in our project, and the whole thing of SpringMVC is automatically configured for us. However, the real project environment is complicated. The built-in configuration of the system may not meet our requirements, and we often need to customize the configuration based on the actual situation.

The custom configuration has to pay attention to, due to the change of the version of Spring Boot, plus this part itself has several different writing methods, many friends here easy to confuse, today Songko to tell you about this problem.

An overview of

First, we need to clarify that there are four main classes and annotations associated with custom SpringMVC:

  • WebMvcConfigurerAdapter
  • WebMvcConfigurer
  • WebMvcConfigurationSupport
  • @EnableWebMvc

Of these four, except for the fourth annotation, the other three are two classes and one interface. The methods in these three seem similar, but the actual use effect is quite different. Therefore, many friends are easy to confuse, today Songo will talk to you about this problem.

WebMvcConfigurerAdapter

The WebMvcConfigurerAdapter is an abstract class that implements the WebMvcConfigurer interface when we customize SpringMVC in Spring Boot 1.x. The abstract class is filled with empty methods. Let’s look at the declaration of this class:

public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    // Various SpringMVC configuration methods
}
Copy the code

Look again at the class comment:

/**
 * An implementation of {@link WebMvcConfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 * @deprecatedAs of 5.0 {@linkWebMvcConfigurer} has default methods (made * possible by a Java 8 baseline) and can be implemented directly without the  * need for this adapter */
Copy the code

This comment is very clear about this class. We can also see that starting with Spring5, since we are going to use Java8, which allows the default method, the official recommendation is to implement the WebMvcConfigurer interface directly. Instead of inheriting WebMvcConfigurerAdapter.

That is, in the era of Spring Boot 1.x, if we needed to customize the SpringMVC configuration, we simply inherited the WebMvcConfigurerAdapter class.

WebMvcConfigurer

WebMvcConfigurer is our solution for implementing custom configurations in Spring Boot 2.x.

WebMvcConfigurer is an interface. The methods in the interface are the same as the empty methods defined in the WebMVCConfigerAdapter, so there is basically no difference in usage. To switch from Spring Boot 1.x to Spring Boot 2.x, simply change the inherited class to the implementation interface.

Songgo in the previous case (40 original dry goods, take you into the Spring Boot Palace!) Where custom SpringMVC configurations are involved, they are also implemented through the WebMvcConfigurer interface.

WebMvcConfigurationSupport

The first two are well understood, there is also a WebMvcConfigurationSupport, what is this used for?

This class is used in the following article:

  • Pure Java code to build the SSM environment

In this article I’ve abandoned the XML configuration files for Spring and SpringMVC and replaced them with Java. So here I custom for SpringMVC configuration, is through inheritance WebMvcConfigurationSupport class. In WebMvcConfigurationSupport class, provides all the methods in Java configuration for SpringMVC need. Let’s take a look at a summary of this method:

A little familiar, you may have noticed that the method here is basically the same as the method in the first two classes.

Here, first of all, everyone to be clear, WebMvcConfigurationSupport class itself is no problem, we can customize for SpringMVC configuration is through inheritance WebMvcConfigurationSupport. But inherit WebMvcConfigurationSupport this operation we only commonly used in Java configuration of SSM project, Spring in the Boot basically won’t write, why?

The WebMvcAutoConfiguration class implements automatic configuration of SpringMVC in Spring Boot. The WebMvcAutoConfiguration class implements automatic configuration of SpringMVC.

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}Copy the code

We can see from the class notes, which has a effective conditions, is when there is no WebMvcConfigurationSupport instance, the automation configuration will take effect. So, if we in the Spring the Boot in the custom for SpringMVC configuration chose WebMvcConfigurationSupport inheritance, will lead to Spring for SpringMVC automation configuration in the Boot failure.

Spring Boot provides us with a lot of automation, and a lot of times when we change these configurations, it’s not to negate the automation that Spring Boot provides, it’s just to make changes to a particular configuration, Other configuration or shall, in accordance with the Spring Boot default configuration to automation, and inherit WebMvcConfigurationSupport to implement for SpringMVC configuration will lead to all the automation configuration for SpringMVC failure, therefore, Normally we don’t choose this option.

In the Java build SSM project (pure Java code structures, SSM environment), because itself is no automation configuration, so we used the WebMvcConfigurationSupport inheritance.

@EnableWebMvc

And finally a @ EnableWebMvc annotations, the annotations is easy to understand, its role is to enable WebMvcConfigurationSupport. Let’s look at the definition of this annotation:

/**
 * Adding this annotation to an {@code @Configuration} class imports the Spring MVC
 * configuration from {@link WebMvcConfigurationSupport}, e.g.:
Copy the code

As you can see, add the annotation, will automatically import WebMvcConfigurationSupport, so in the Spring in the Boot, we do not recommend the use of @ EnableWebMvc annotations, Because it also invalidates the Automatic configuration of SpringMVC in Spring Boot.

conclusion

Do you understand the above explanation? Let me summarize this briefly:

  1. In Spring Boot 1.x, a custom SpringMVC configuration can be implemented by inheriting the WebMvcConfigurerAdapter.
  2. In Spring Boot 2.x, custom SpringMVC configuration can be accomplished by implementing the WebMvcConfigurer interface.
  3. If in the Spring in the Boot using inheritance WebMvcConfigurationSupport to implement custom for SpringMVC configuration, or in the Spring in the Boot using the @ EnableWebMvc annotations, The default SpringMVC automation configuration in Spring Boot is invalidated.
  4. In pure Java configuration of SSM environment, if we want to custom for SpringMVC configuration, there are two ways, one is directly inherited from WebMvcConfigurationSupport to complete for SpringMVC configuration, Another option is to implement the WebMvcConfigurer interface to customize the SpringMVC configuration. If you use the second option, you need to add an additional @enableWebMVC annotation to the SpringMVC configuration class. Said to enable WebMvcConfigurationSupport, this configuration will only take effect. In other words, in pure Java configuration of SSM, if you need a custom for SpringMVC configuration, you can’t leave WebMvcConfigurationSupport, So in this case suggest through inheritance WebMvcConfigurationSupport configuration to realize automation.

I wonder if you can understand it. If you have any questions, please leave a comment.

Pay attention to the public account [Jiangnan little Rain], focus on Spring Boot+ micro service and front and back end separation and other full stack technology, regular video tutorial sharing, after attention to reply to Java, get Songko for you carefully prepared Java dry goods!