WebMvcConfigurationSupport和WebMvcConfigurerAdapter之间的区别


问题内容

我想添加资源处理程序。在论坛上,他们使用WebMvcConfigurationSupporthttp :
//forum.springsource.org/showthread.php?116068-How-to-configure-lt-mvc-
resources-gt-mapping-to-take-precedence-over-
RequestMapping&p=384066#第384066章

和文档说WebMvcConfigurerAdapterhttp :
//static.springsource.org/spring/docs/3.2.x/javadoc-
api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html

有什么区别,使用哪一个?两者都有addResourceHandlers我需要的方法。

这是我目前的课程:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    public @Override void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
    }

    public @Bean TilesViewResolver tilesViewResolver() {
        return new TilesViewResolver();
    }

    public @Bean TilesConfigurer tilesConfigurer() {
        TilesConfigurer ret = new TilesConfigurer();
        ret.setDefinitions(new String[] { "classpath:tiles.xml" });
        return ret;
    }
}

问题答案:

答案在您上面引用的文档中:

如果WebMvcConfigurer的自定义选项未提供您需要配置的内容,请考虑删除@EnableWebMvc批注,并直接从WebMvcConfigurationSupport进行扩展以覆盖选定的@Bean方法

简而言之,如果@EnableWebMvc为您工作,则无需进一步查找。