注释@EnableSpringDataWebSupport不适用于WebMvcConfigurationSupport吗?

akc*_*soy 5 spring spring-mvc spring-java-config spring-data-commons

我已经使用WebMvcConfigurerAdapter已有一段时间了。由于无法使用getInterceptors()方法获取所有已注册的拦截器,因此我切换到了WebMvcConfigurationSupport,它具有许多默认的已注册Spring Bean,例如ContentNegotiationManager,ExceptionHandlerExceptionResolverusw。

现在,我已经意识到,尽管我在WebConfig类上使用了@EnableSpringDataWebSupport批注,但非常方便的DomainClassConverter(使用CrudRepository将域类ID转换为域类对象)并未默认注册。

当我像这样显式定义此bean时,它就会起作用。

@EnableSpringDataWebSupport
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public DomainClassConverter<?> domainClassConverter() {
        return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,为什么EnableSpringDataWebSupport无法与WebMvcConfigurationSupport一起使用?

kyl*_*esm 3

看起来WebMvcConfigurationSupport直接扩展的配置类受到SPR-10565的影响。至少对我来说,解决方案是从DelegatingWebMvcConfiguration替代延伸。

如果您要覆盖配置类中的各个回调,您可能还需要调用超类的回调实现,以确保所有回调都得到正确处理。