Reputation: 661
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
Upvotes: 4
Views: 6706
Reputation: 2333
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.
Upvotes: 0
Reputation: 52516
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.
Upvotes: 3
Reputation: 661
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
Upvotes: 1
Reputation: 21401
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
)
Upvotes: 10