kokojustin
kokojustin

Reputation: 200

what is the difference between using @EnableWebMvc + WebMvcConfigurer and WebMvcConfigurer?

what is the difference between using @EnableWebMvc + WebMvcConfigurer and WebMvcConfigurer??

@Configuration
@EnableWebMvc
class WebMvcConfig(): WebMvcConfigurer {}
@Configuration
class WebMvcConfig(): WebMvcConfigurer {}

Upvotes: 0

Views: 2037

Answers (1)

M. Deinum
M. Deinum

Reputation: 124622

@Configuration
class WebMvcConfig(): WebMvcConfigurer {}

This will only take effect if @EnableWebMvc is used. The WebMvcConfigurer instancs are detected by an instance of DelegatingWebMvcConfiguration which is registered through @EnableWebMvc. So for this to work you need @EnableWebMvc.

NOTE: When using Spring Boot, this is automatically done when Spring Boot detects Spring MVC classes on the classpath!.

Without @EnableWebMvc a WebMvcConfigurer doesn't do anything but take up memory. When not using @EnableWebMvc the DispatcherServlet will install some defaults. Those defaults are hardcoded in the DispatcherServlet.properties file. Those defaults are hard to modify without the use of @EnableWebMvc.

WARNING: When using Spring Boot andadding @EnableWebMvc, will actually disable a large part of the MVC auto-configuration done by Spring Boot, which can lead to other surprises!

Upvotes: 5

Related Questions