是否可以扩展WebMvcConfigurationSupport并使用WebMvcAutoConfiguration? [英] Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?

查看:384
本文介绍了是否可以扩展WebMvcConfigurationSupport并使用WebMvcAutoConfiguration?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要扩展WebMvcConfigurationSupport类,修改两件事:

I need to extend the WebMvcConfigurationSupport class too modify two things:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
        handlerMapping.setRemoveSemicolonContent(false);
        handlerMapping.setOrder(1);
        return handlerMapping;
    }
}

我喜欢从WebMvcAutoConfiguration类注册的默认值但是由于类上的条件注释,当我扩展WebMvcConfigurationSupport类时,它会阻止自动配置发生。

I like the defaults that are registered from the WebMvcAutoConfiguration class but due to the conditional annotation on the class, when I extend the WebMvcConfigurationSupport class it prevents the auto configuration from happening.

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
    WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class WebMvcAutoConfiguration {...}

是否要在没有WebMvcAutoConfiguration类的情况下加载基本上复制/粘贴该类中的大部分代码?

Is there to have the WebMvcAutoConfiguration class load without having to essentially copy/paste most of the code in that class?

或者是否可以从其他地方调用RequestMappingHandlerMapping setOrder()和setRemoveSemicolonContent(),这样我就可以使用@EnableWebMvc注释并运行自动配置类而不会出现任何问题?

Or is it possible to call RequestMappingHandlerMapping setOrder() and setRemoveSemicolonContent() from somewhere else so I can just use the @EnableWebMvc annotation and have the autoconfiguration class run without any issues?

提前致谢!

推荐答案

您的分析是正确的( @EnableWebMvc 或直接扩展 WebMvcConfigurationSupport 将关闭 WebMvcAutoConfiguration )。我不确定替代方案是什么,因为a)我们需要autoconfig的get-out子句,以及b)我不认为Spring喜欢有两个 WebMvcConfigurationSupports 在相同的上下文中。很高兴在github上讨论,如果你想尝试找到改变它的方法(可能有一些中间立场)。

Your analysis is correct (@EnableWebMvc or directly extending WebMvcConfigurationSupport will switch off the WebMvcAutoConfiguration). I'm not sure what the alternative is, since a) we need a "get-out" clause for the autoconfig, and b) I don't think Spring likes to have two WebMvcConfigurationSupports in the same context. Happy to discuss on github if you want to try and find a way to change it (there might be some middle ground).

这篇关于是否可以扩展WebMvcConfigurationSupport并使用WebMvcAutoConfiguration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆