項目集成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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章