首页 文章

WebMvcConfigurationSupport和WebMvcConfigurerAdapter之间的区别

提问于
浏览
28

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

和文档说 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;
    }
}

3 回答

  • 3

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

    如果WebMvcConfigurer的自定义选项没有公开您需要配置的内容,请考虑删除@EnableWebMvc注释并直接从WebMvcConfigurationSupport扩展,覆盖所选的@Bean方法

    简而言之,如果@EnableWebMvc适合您,则无需进一步查看 .

  • 2

    它更好地扩展WebMvcConfigurationSupport . 它提供了更多的自定义选项,也可以正常使用

    configureMessageConverters(List<HttpMessageConverter<?>> converters)
    

    因为你可以使用添加这些转换器

    addDefaultHttpMessageConverters(converters);
    

    WebMvcConfigurerAdapter不具备此功能 .

    点击[这里] How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?

    如果扩展WebMvcConfigurerAdapter,它在配置Jackson和Jaxb时会表现得很奇怪 . 发生在我身上!!!

  • 22

    如果您使用ConfigurationSupport类,那么在尝试提供静态资源时,请记住麻烦的麻烦,因为它不起作用 .

相关问题