由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext [英] Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

查看:50209
本文介绍了由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i run the application using main application, i got the error in consoleUnable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

主应用程序

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

Servlet初始化程序

Servlet Initializer

public class ServletInitializer extends SpringBootServletInitializer {

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

build.gradle

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'
}

帮帮我

推荐答案

可能会有所帮助,请参阅这个答案,感谢 Andres Cespedes Morales

May be this can help, refer to this answer, thanks to Andres Cespedes Morales:

这条消息说明:你需要在ApplicationContext中配置至少1个ServletWebServerFactory bean ,所以如果你已经有了spring-boot-starter-tomcat,你需要自动配置该bean或手动执行。

This message says: You need to configure at least 1 ServletWebServerFactory bean in the ApplicationContext, so if you already have spring-boot-starter-tomcat you need to either autoconfigure that bean or to do it manually.

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

So, in the test there are only 2 configuration classes to load the applicationContext, these are = { WebsocketSourceConfiguration.class, WebSocketSourceIntegrationTests.class }, then at least in one of these classes there should be a @Bean method returning an instance of the desired ServletWebServerFactory.


  • SOLUTION *

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

Make sure to load all the beans within your configuration class

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

或者还启用自动配置进行类路径扫描和自动配置这些bean。

OR also enable the AutoConfiguration to do a classpath scanning and auto-configuration of those beans.

@EnableAutoConfiguration
WebsocketSourceConfiguration

也可以在Integration Test类中完成。

Can be done also at the Integration Test class.

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

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

For more information check the SpringBootTest annotation documentation https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

这篇关于由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆