Unable to start ServletWebServerApplicationContext

2019-01-23 19:57发布

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.

Main Application

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

Servlet Initializer

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

Help Me

1条回答
祖国的老花朵
2楼-- · 2019-01-23 20:36

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

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.

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 *

Make sure to load all the beans within your configuration class

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

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

@EnableAutoConfiguration
WebsocketSourceConfiguration

Can be done also at the Integration Test class.

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

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

查看更多
登录 后发表回答