由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext

问题描述 投票:8回答:1

当我使用主应用程序运行应用程序时,我在consoleUnable中得到错误以启动Web服务器;嵌套异常是org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext。

主要应用

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Servlet初始化程序

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

的build.gradle

    buildscript {
        ext {
            springBootVersion = '2.0.0.M4'
        }
        repositories {
            jcenter()
            mavenCentral()
            maven { url "https://repo.spring.io/snapshot" }
            maven { url "https://repo.spring.io/milestone" }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    plugins {
        id "org.sonarqube" version "2.5"
    }

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'eclipse-wtp'
    apply plugin: 'jacoco'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'war'


    group = 'com.demo'
    version = '0.0.1-SNAPSHOT'

    // Uses JDK 8
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        maven { url "https://repo.spring.io/milestone" }
        jcenter()
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
    }
    configurations {
        providedRuntime
    }
    dependencies {

        // SPRING FRAMEWORK
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.springframework.boot:spring-boot-starter-aop')
        compile('org.springframework.boot:spring-boot-starter-actuator')

        // Tomcat Server
        providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

        //Spring Jpa
        compile('org.springframework.boot:spring-boot-starter-data-jpa')

        // SPRING SECURITY
        compile('org.springframework.boot:spring-boot-starter-security')

        // MYSQL and HIBERNATE
        compile 'mysql:mysql-connector-java:5.1.34'
        //compile 'org.hibernate:hibernate-core:5.2.11.Final'
        //compile 'org.hibernate:hibernate-validator:5.1.3.Final'
}

帮我

java tomcat spring-boot intellij-idea
1个回答
5
投票

可能这可以帮助,参考this answer,感谢Andres Cespedes Morales

此消息说:您需要在ApplicationContext中配置至少1个ServletWebServerFactory bean,因此如果您已经有spring-boot-starter-tomcat,则需要自动配置该bean或手动执行此操作。

因此,在测试中只有2个配置类来加载applicationContext,这些是= {WebsocketSourceConfiguration.class,WebSocketSourceIntegrationTests.class},那么至少在其中一个类中应该有一个@Bean方法返回所需的实例ServletWebServerFactory。

  • 解决方案

确保加载配置类中的所有bean

WebsocketSourceConfiguration {
  @Bean 
  ServletWebServerFactory servletWebServerFactory(){
  return new TomcatServletWebServerFactory();
  }
}

或者还使AutoConfiguration能够对这些bean进行类路径扫描和自动配置。

@EnableAutoConfiguration
WebsocketSourceConfiguration

也可以在Integration Test类中完成。

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

有关更多信息,请查看SpringBootTest注释文档https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

© www.soinside.com 2019 - 2024. All rights reserved.