当我使用主应用程序运行应用程序时,我在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'
}
帮帮我
答案 0 :(得分: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