Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?

后端 未结 4 1535
时光取名叫无心
时光取名叫无心 2020-12-08 08:28

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

@Configuration
public class WebConfig e         


        
相关标签:
4条回答
  • 2020-12-08 09:06

    I think the best way to do this in Spring Boot now is to add a WebMvcRegistrations component to your context - this solution didn't exist at the time of your question (it's been available since Spring Boot 1.4.0).

    0 讨论(0)
  • 2020-12-08 09:08

    Extend from DelegatingWebMvcConfiguration instead of WebMvcConfigurationSupport, it will not prevent the autoconfig to take place:

    @Configuration
    public class WebConfig extends DelegatingWebMvcConfiguration {
        @Override
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
            RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
            handlerMapping.setRemoveSemicolonContent(false);
            handlerMapping.setOrder(1);
            return handlerMapping;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 09:09

    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).

    0 讨论(0)
  • 2020-12-08 09:12

    I managed to customize the RequestMappingHandlerMapping while keeping WebMvcAutoConfiguration using a BeanPostProcessor:

    @Configuration
    public class RequestMappingConfiguration {
        @Bean
        public RequestMappingHandlerMappingPostProcessor requestMappingHandlerMappingPostProcessor() {
            return new RequestMappingHandlerMappingPostProcessor();
        }
    
        public static class RequestMappingHandlerMappingPostProcessor implements BeanPostProcessor {
    
            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof RequestMappingHandlerMapping) {
                    ((RequestMappingHandlerMapping) bean).setUseSuffixPatternMatch(false);
                }
                return bean;
            }
    
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                return bean;
            }
        }
    }
    

    I would be happy if Spring Boot provides a better way to handle that... maybe something could be done around PathMatchConfigurer ?

    0 讨论(0)
提交回复
热议问题