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

3okqufwl  于 2023-05-28  发布在  Spring
关注(0)|答案(2)|浏览(166)

我正在构建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')
jhiyze9q

jhiyze9q1#

我正在创建ServletContextInitializer bean,并在onStartup方法中创建AnnotationConfigWebApplicationContext。
因此,我错过了为这个WebApplicationContext设置ServletContext,这给我造成了巨大的破坏。
所以最后为这个AnnotationConfigWebApplicationContext对象设置setServletContext(servletContext),我就解决了我的问题。

cbeh67ev

cbeh67ev2#

在我的案例中,错误是由Intellij Idea中Gradle项目中的冗余/out/build文件夹引起的。
IntelliJ Idea提供了两种构建和运行Gradle项目的方法。通过IntelliJ内部编译和运行-或者通过将构建和执行委托给Gradle。
它们中的每一个都为编译的类创建自己的输出文件夹。
IntelliJ - /out Gradle - /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()
                }
        }
    }
}

相关问题