项目集成swagger启动报错 Error creating bean with name webMvcRequestHandlerProvider

最近项目集成了swagger以后启动一直报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/jasonfeng/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

网上研究了半天找到了解决方案。

首先要从spring的启动顺序讲起,spring是先加载applicationContext.xml然后再执行spring-servlet.xml(web.xml里配置)。

然后spring(applicationContext.xml)和spring-mvc(spring-servlet.xml)是两个容器。父容器(spring)读不到子容器的bean
Spring 和SpringMVC 的父子容器关系

而swagger的加载是在spring-servlet.xml配置加载的。如果我们在applicationContext.xml里也加载了swagger的话,那么会读不到一些配置在子容器的配置

例如我applicationContext.xml和spring-servlet.xml都组件扫描了swaggerConfig,

spring-servlet.xml

<context:component-scan base-package="com.olymtech.fms.air.config.swagger"></context:component-scan>

applicationContext.xml

<context:component-scan base-package="com.olymtech.fms"/>

解决办法:

applicationContext.xml去除扫描swaggerConfig

<context:component-scan base-package="com.olymtech.fms">
        <context:exclude-filter type="assignable" expression="com.olymtech.fms.air.config.swagger.SwaggerConfig"/>
    </context:component-scan>
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值