spring WebMvcConfigurationSupport 和 WebMvcConfigurerAdapter 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17898606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:07:18  来源:igfitidea点击:

Difference between WebMvcConfigurationSupport and WebMvcConfigurerAdapter

springspring-mvc

提问by dtrunk

I would like to add resource handlers. In the forum they use WebMvcConfigurationSupport: http://forum.springsource.org/showthread.php?116068-How-to-configure-lt-mvc-resources-gt-mapping-to-take-precedence-over-RequestMapping&p=384066#post384066

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

and docs say WebMvcConfigurerAdapter: http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html

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

What's the difference and which one to use? Both has the addResourceHandlersmethod I need.

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

This is my current class:

这是我目前的课程:

@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;
    }
}

采纳答案by Rossen Stoyanchev

The answer is in the doc you referenced above:

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

If the customization options of WebMvcConfigurer do not expose something you need to configure, consider removing the @EnableWebMvc annotation and extending directly from WebMvcConfigurationSupport overriding selected @Bean methods

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

In short, if @EnableWebMvc works for you, there is no need to look any further.

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

回答by Pushpendra Jaiswal

Its better to extend WebMvcConfigurationSupport. It provides more customization options and also works fine with

最好扩展 WebMvcConfigurationSupport。它提供了更多的自定义选项,也适用于

configureMessageConverters(List<HttpMessageConverter<?>> converters) 

cause you can add these convertors using

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

addDefaultHttpMessageConverters(converters);

that is not available with WebMvcConfigurerAdapter.

这不适用于 WebMvcConfigurerAdapter。

Click [here] How to configure MappingHymansonHttpMessageConverter while using spring annotation-based configuration?

点击【这里】在使用基于spring注解的配置时如何配置MappingHymansonHttpMessageConverter?

If you extend WebMvcConfigurerAdapter, it behaves strangely with configuring Hymanson and Jaxb. That happened with me !!!

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

回答by user1363516

if you use ConfigurationSupport class get ready mind numbing hardwork when trying to serve static resources, because it does not work.

如果您使用 ConfigurationSupport 类,请在尝试提供静态资源时准备好麻木的努力,因为它不起作用。

回答by virgo47

I was recently solving this very same problem when configuring converters and it resulted in quite a long post.

我最近在配置转换器时解决了这个非常相同的问题,结果写了很长的帖子

By default Spring Boot uses its implementation of WebMvcConfigurationSupportand does a lot of auto-magic including finding all the WebMvcConfigurerand using them. There is one implementation provided by Boot already and you may add more. This results in seemingly confusing behaviour when the list of converters coming to configureMessageConvertersin your implementation of WebMvcConfigureris already pre-populated from previous configurer.

默认情况下,Spring Boot 使用它的实现WebMvcConfigurationSupport并做了很多自动魔术,包括找到所有的WebMvcConfigurer和使用它们。Boot 已经提供了一种实现,您可以添加更多。当configureMessageConverters您的实现中的转换器列表WebMvcConfigurer已经从先前的配置器中预先填充时,这会导致看似混乱的行为。

These types (WebMvcConfigurationSupportand WebMvcConfigurer) have also strikingly similar interface - but the first does NOT implement the other. The point is:

这些类型(WebMvcConfigurationSupportWebMvcConfigurer)也有惊人相似的接口——但第一个没有实现另一个。重点是:

Supportclass searches for configurersand uses them + does something on its own.

支持类搜索配置器并使用它们 + 自己做一些事情。

If you extend from WebMvcConfigurationSupportyou take over the configuration and while there are some things available that are not in WebMvcConfigurer(like addDefaultHttpMessageConverters) there is also tons of code from EnableWebMvcConfigurationand DelegatingWebMvcConfigurationthat does not happen.

如果你扩展WebMvcConfigurationSupport你接管配置,虽然有一些可用的东西WebMvcConfigurer(比如addDefaultHttpMessageConverters),但也有大量的代码EnableWebMvcConfiguration并且DelegatingWebMvcConfiguration不会发生。

Both extending WebMvcConfigurationSupportor WebMvcConfigurer(not sure both at once makes much sense) have their valid usages, but with extending the support class you take over the process much more and lose a lot of "opinionated" Spring Boot functionality.

扩展WebMvcConfigurationSupportWebMvcConfigurer(不确定两者都有意义)都有其有效的用法,但是通过扩展支持类,您可以更多地接管该过程并失去许多“固执己见”的 Spring Boot 功能。