4

I would like to add resource handlers using WebMvcConfigurerAdapter in Windows, but in Linux it doesn't work, so I add WebMvcConfigurationSupport.

After debug and test I find two bean will be create in both OS, but the override function of WebMvcConfigurerAdapter will be executed only at Windows and the override function of WebMvcConfigurationSupport will be executed only at Linux.

I can't find out the reason. The two configuration classes are shown below:

@Configuration
public class JxWebAppConfigurer  extends WebMvcConfigurerAdapter {
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
     registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
     super.addResourceHandlers(registry);
   }
}

This is the other one:

@Configuration
public class JxWebConfiguration extends WebMvcConfigurationSupport {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
       super.addResourceHandlers(registry);
   }
}

@EnalbeMvc is already been added at the main class

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
ysjiang
  • 623
  • 6
  • 11
  • Possible duplicate of [Difference between WebMvcConfigurationSupport and WebMvcConfigurerAdapter](https://stackoverflow.com/questions/17898606/difference-between-webmvcconfigurationsupport-and-webmvcconfigureradapter) – tkruse Aug 15 '19 at 06:24

4 Answers4

10

As mentioned in the @EnableWebMvc Documentation:

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport

{..}

To customize the imported configuration, implement the interface WebMvcConfigurer or more likely extend the empty method base class WebMvcConfigurerAdapter and override individual methods

{..}

If WebMvcConfigurer does not expose some advanced setting that needs to be configured, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport

So in effect either:

  1. @EnableWebMvc + extending WebMvcConfigurerAdapter (suggested first option)
  2. Extending directly from WebMvcConfigurationSupport (fallback alternative for full control)

(on both cases needed @Configuration)

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • Ok,I read the documents last night and test again, I find another @EnableWebMvc that I don't find before,it's the problems, one @@EnableWebMvc is enough but I add several and ignore it. – ysjiang Dec 01 '16 at 02:06
  • If you find this answer useful please consider upvoting/accepting it. – dimitrisli Dec 01 '16 at 02:09
  • I have another question: Can I extends WebMvcConfigurationSupport(or DelegatingWebMvcConfiguration,what's the difference between them?) and still use WebAutoConfiguration? Or there is another way to add resourcehandlers and don't switch off WebAutoConfiguration? Many thanks~ – ysjiang Dec 01 '16 at 03:37
3

The alternative solution for org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapteris org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport (I am using Spring Framework 5.0.2.RELEASE).

WebMvcConfigurerAdapter has been deprecated.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • 2
    This is not true - alternative for now deprecated `WebMvcConfigurerAdapter` is interface `WebMvcConfigurer` with its default methods now. Javadoc says that as well. Extending from `WebMvcConfigurationSupport` has side effects; behavior is different. – virgo47 May 14 '20 at 07:12
1

I know the reasons. As mentioned above,you should choose one select(extends WebMvcConfigurerAdapter+@EnableWebMvc or just extends WebMvcConfigurationSupport ) ;

Never use @EnableWebMvc and extending WebMvcConfigurationSupport together!!

if use spring-boot's @EnableAutoConfiguration ,you can just extends WebMvcConfigurerAdapter and don't use @EnableMvc

ysjiang
  • 623
  • 6
  • 11
0

Behavior differences on OS are not clear to me (classpath order?) so I'll just talk about WebMvcConfigurationSupport vs WebMvcConfigurer. Let's start with WebMvcConfigurerAdapter that implements WebMvcConfigurer, but now is deprecated because the interface has the functionality via default methods.

Now to the "support" (extending WebMvcConfigurationSupport) vs "configurer" (implementing WebMvcConfigurer). These classes have very similar methods but it works roughly like this:

Support component finds all the configurers and combines them into the final configuration.

I recently wrote quite a long post about this with code examples and I recommend experimenting with small Spring Boot applications and debug them - it's eye opening.

Boot has a default implementation of WebMvcConfigurationSupport and it does a lot of stuff - including finding beans implementing WebMvcConfigurer and using them. If you take over and implement the support class, Boot will find it, disables the default one and you're in full control. But then a lot of default auto-magic is gone and you have to use it if needed.

virgo47
  • 2,283
  • 24
  • 30