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
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:
@EnableWebMvc
+ extending WebMvcConfigurerAdapter
(suggested first option)WebMvcConfigurationSupport
(fallback alternative for full control)(on both cases needed @Configuration
)
The alternative solution for org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
is org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
(I am using Spring Framework 5.0.2.RELEASE).
WebMvcConfigurerAdapter
has been deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With