启动时报错,加载了两个相同的bean:requestMappingHandlerMapping,controllerEndpointHandlerMapping
问题
启动时报错,加载了两个相同的bean:requestMappingHandlerMapping,controllerEndpointHandlerMapping
提供详细日志、截图、回显步骤
PermitAllUrlResolver.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available: expected single matching bean but found 2: requestMappingHandlerMapping,controllerEndpointHandlerMapping
解决方案
位置:PermitAllUrlResolver类中afterPropertiesSet()方法
修改前代码:RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
修改后代码:RequestMappingHandlerMapping mapping = applicationContext.getBean("requestMappingHandlerMapping",RequestMappingHandlerMapping.class);
说明:原来只使用了class类型获取bean,但是controllerEndpointHandlerMapping是requestMappingHandlerMapping的子类,使用applicationContext.getBean会返回两个bean导致报错,所以在applicationContext.getBean获取bean时加入name属性即可解决问题。