作者:2012hellen_887 | 来源:互联网 | 2023-01-16 13:00
如何解决《由于缺少ServletWebServerFactorybean,无法启动ServletWebServerApplicationContext》经验,为你挑选了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'
}
帮我
1> 小智..:
可能对您有所帮助,请参考此答案,多亏了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();
}
}
或还使自动配置能够对这些bean进行类路径扫描和自动配置。
@EnableAutoConfiguration
WebsocketSourceConfiguration
也可以在集成测试课程中完成。
@EnableAutoConfiguration
WebSocketSourceIntegrationTests
有关更多信息,请查看SpringBootTest注释文档https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html