Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confuse between WebMvcConfigurationSupport and WebMvcConfigurerAdapter

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

like image 286
ysjiang Avatar asked Dec 10 '22 14:12

ysjiang


2 Answers

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)

like image 162
dimitrisli Avatar answered Dec 13 '22 03:12

dimitrisli


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.

like image 30
Do Nhu Vy Avatar answered Dec 13 '22 03:12

Do Nhu Vy