如何设置detectHandlerMethodsInAncestorContexts为真?

时间:2015-11-26 14:49:42

标签: spring spring-mvc controller

在spring-webmvc中,AbstractHandlerMethodMapping.java中有一个参数detectHandlerMethodsInAncestorContexts

其默认值为false。我怎样才能将其设置为真?

2 个答案:

答案 0 :(得分:0)

在春季4(引导1.5)中,这对我有用:

@Configuration
@ComponentScan(basePackages = "my.app")
class WebpageContext extends WebMvcConfigurationSupport {
    @Override
    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
       RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
       mapping.setDetectHandlerMethodsInAncestorContexts(true); 
       return mapping;
    }
}

(通常,您用@Configuration注释@EnableWebMvc类并扩展@WebMvcConfigurerAdapter,但是它不公开请求映射,因此我们需要直接扩展WebMvcConfigurationSupport。)

答案 1 :(得分:-1)

只需显式创建RequestMappingHandlerMapping bean并将此值设置为true

@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping();
    requestMappingHandlerMapping.setDetectHandlerMethodsInAncestorContexts(true);
    return requestMappingHandlerMapping;
}