使用springboot创建名为'documentationPluginsBootstrapper'的bean时出错

时间:2017-08-23 06:05:09

标签: spring-boot

我正在构建springboot应用程序,但在启动应用程序时,它会引发异常。 我正在通过ServletContextInitializer bean配置ServletContext。 任何帮助或意见将不胜感激。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [..!/BOOT-INF/lib/springfox-spring-web-2.6.1.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 6; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.ServletContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1056)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.filter.DelegatingFilterProxy.findWebApplicationContext(DelegatingFilterProxy.java:297)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:233)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:109)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4572)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.ServletContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 26 more

下面是gradle依赖。我希望它会有所帮助。

compile(
                "javax.validation:validation-api:1.1.0.Final",
                "org.hibernate:hibernate-validator:5.1.0.Final",
                "org.glassfish.web:el-impl:2.2",

                'org.bouncycastle:bcprov-jdk15on:1.51',
                'ibmjceprovider:ibmjceprovider:ibmjceprovider',
                'com.google.guava:guava:18.0'
        )
        compile('com.googlecode.lambdaj:lambdaj:2.3.3') {
            exclude group: 'org.hamcrest', module: 'hamcrest-all'
        } 
       compile('io.springfox:springfox-swagger2:2.4.0')
        compile('io.springfox:springfox-swagger-ui:2.4.0')
        compile 'io.jsonwebtoken:jjwt:0.7.0'
        runtime(
                'org.apache.httpcomponents:httpasyncclient:4.0.2'
        )
       configurations {
        all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        all*.exclude group: 'org.springframework', module: 'spring-orm'
        all*.exclude group: 'org.springframework.messaging', module: 'spring-messaging'
        all*.resolutionStrategy {
            force 'com.jayway.jsonpath:json-path:1.1.0'
        }
       compile(           
                'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.10',
                'org.springframework.boot:spring-boot-starter-web'
        )
        testCompile('org.springframework.boot:spring-boot-starter-test')

2 个答案:

答案 0 :(得分:0)

我正在创建ServletContextInitializer bean,在onStartup方法中我创建了AnnotationConfigWebApplicationContext。

所以我缺少为这个WebApplicationContext设置ServletContext,这对我来说是一场彻底的破坏。

所以最后为这个AnnotationConfigWebApplicationContext对象设置setServletContext(servletContext)我摆脱了我的问题。

答案 1 :(得分:0)

就我而言,该错误是由Intellij Idea中Gradle项目中的多余/out/build文件夹引起的。

IntelliJ Idea提供了两种构建和运行Gradle项目的方法。通过IntelliJ内部编译和运行-或将构建和执行委托给Gradle。

它们每个都为编译的类创建自己的输出文件夹。

IntelliJ-/out 摇篮-/build

当您切换构建运行的方式时(从Gradle到IntelliJ,反之亦然),另一个输出文件夹仍然保留-导致重复的类出现在类路径上。

不幸的是,这两个文件夹都在IntelliJ的类路径上: sample folder structure in IntelliJ

对于我来说,运行Gradle clean或删除所有/out文件夹确实有用。

这是一项Gradle任务,用于删除整个项目树中的/out文件夹(如果它是一个多项目):

task deleteOutFolders {
    doLast {
        fileTree(dir: ".").visit {
            FileVisitDetails details ->
                if (details.directory && details.file.name == "out") {
                    println "deleting ${details}"
                    details.file.deleteDir()
                }
        }
    }
}