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

时间:2025-03-17 07:13:15

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

: 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.!/springfox/documentation/spring/web/plugins/]: Unsatisfied dependency expressed through constructor argument with index 0 of type []: : No qualifying bean of type [] found for dependency [collection of ]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is : No qualifying bean of type [] found for dependency [collection of ]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

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

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

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

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

例如我和都组件扫描了swaggerConfig,

<context:component-scan base-package=""></context:component-scan>

<context:component-scan base-package=""/>

解决办法:

去除扫描swaggerConfig

<context:component-scan base-package="">
        <context:exclude-filter type="assignable" expression=""/>
    </context:component-scan>