expected single matching bean but found 2: requestMappingHandlerMapping。。。(已解决)

问题原因

因为我有两个大模块,一个模块要重写MVC配置,要配置静态资源以及模板引擎,另一个模块要注入插件配置,然后稀里糊涂的一顿操作就这样了。

配置MVC

public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {
}

插件配置

……
public PluginControllerPostProcessor(ApplicationContext applicationContext) {
        Objects.requireNonNull(applicationContext);
        this.springBeanRegister = new SpringBeanRegister(applicationContext);
        this.applicationContext = (GenericApplicationContext) applicationContext;
        this.requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        this.integrationConfiguration = applicationContext.getBean(IntegrationConfiguration.class);
}
……

然后就出现了expected single matching bean but found 2: requestMappingHandlerMapping的报错,我把this.requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);屏蔽了就正常,说明从这之前就已经存在两个RequestMappingHandlerMapping对象了,但是屏蔽了模板引擎也能用,很奇怪了。

解决办法

当我把MVC配置继承的类改成接口WebMvcConfigurer就神特么好使了,原因未知,毕竟是技术小白。

public class WebMvcAutoConfiguration implements WebMvcConfigurer {
}
# java   code   springboot  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×