GVKun编程网logo

Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

7

对于SpringBootTest失败说,由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext感兴趣的读者,本文将

对于Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于AnnotationConfigServletWebServerApplicationContext解析、ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext、ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext、idea spring boot启动报missing ServletWebServerFactory错记录的宝贵知识。

本文目录一览:

Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

测试类别:-

@RunWith(SpringRunner.class)@SpringBootTest(classes = { WebsocketSourceConfiguration.class,        WebSocketSourceIntegrationTests.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {                "websocket.path=/some_websocket_path", "websocket.allowedOrigins=*",                "spring.cloud.stream.default-binder=kafka" })public class WebSocketSourceIntegrationTests {    private String port = "8080";    @Test    public void testWebSocketStreamSource() throws IOException, InterruptedException {        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();        ClientWebSocketContainer clientWebSocketContainer = new ClientWebSocketContainer(webSocketClient,                "ws://localhost:" + port + "/some_websocket_path");        clientWebSocketContainer.start();        WebSocketSession session = clientWebSocketContainer.getSession(null);        session.sendMessage(new TextMessage("foo"));        System.out.println("Done****************************************************");    }}

我在这里看到过同样的问题,但没有任何帮助。我可以知道我在想什么吗?

spring-boot-starter-tomcat在依赖关系层次结构中具有编译时依赖关系。

答案1

小编典典

该消息表明: 您需要在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进行类路径扫描和自动配置。

@EnableAutoConfigurationWebsocketSourceConfiguration

也可以在集成测试课程中完成。

@EnableAutoConfigurationWebSocketSourceIntegrationTests

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

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

springboot中启动的applicationContext就是这个类。类图:

getBean方法

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(requiredType);
	}

	public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
	}

	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}

getBean方法的具体实现主要是通过beanFactory来实现的。

DefaultListableBeanFactory

类图

getBean具体的ioc流程后续分析

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		return getBean(requiredType, (Object[]) null);
	}

ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

我已经使用Spring Boot编写了Spring Batch应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在linux服务器上运行它时,出现以下异常

Unable to start web server; nested exception isorg.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

下面是我的运行方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

答案1

小编典典

解决方案是:

我明确下面的属性设置为noneapplication.yml文件中。

spring:  main:    web-application-type: none

答案2

小编典典

可能你缺少@SpringBootApplicationSpring Boot入门类。

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

ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext

ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext

我已经使用Spring Boot编写了Spring
Batch应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在linux服务器上运行它时,出现以下异常

Unable to start web server; nested exception isorg.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

下面是我的运行方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

答案1

小编典典

解决方案是:

noneapplication.yml文件中明确设置了以下属性。

spring:  main:    web-application-type: none

idea spring boot启动报missing ServletWebServerFactory错记录

idea spring boot启动报missing ServletWebServerFactory错记录

IDEA 启动springboot项目报missing ServletWebServerFactory

大概就是说加载不到ServletWebServerFactory,IDEA定位下,可以发现,这个工厂接口有3个实现类JettyServletWebServerFactory,TomcatServletWebServerFactory,UndertowServletWebServerFactory。
回到问题,无法实例化Bean出现的错误,那么是不是可以考虑手动标记呢?

给启动类添加上Bean声明,也就是当启动时,web服务容器会加载这么一个bean

@Bean ServletWebServerFactory servletWebServerFactory() { retrun new TomcatServletWebServerFactory(); }

运行项目,发现依然报错了,万幸的是,这次错误信息有变化了,很长一段。 核心错误信息是Caused by: java.lang.ClassNotFoundException: org.apache.catalina.core.AprLifecycleListener. 继续定位该类AprLifecycleListener,发现是tomcat-embed-core-9.0.19.jar包下的东西,那为何会找不到呢? 这里的scope配置的是provide,也就是说,在编译打包时,lib里不会存在这个artifact,以防止与容器冲突。 然而在idea的时候,却因此读取不到这个class导致了问题(具体原因目前已触及我知识盲区)。 总之,把scope的值改为compile(默认值)即可,到发布正式环境时记得改回去就可以了(不需要配置TomcatServletWebServerFactory bean)。

关于Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于AnnotationConfigServletWebServerApplicationContext解析、ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext、ApplicationContextException:由于缺少ServletWebServerFactorybean而无法启动ServletWebServerApplicationContext、idea spring boot启动报missing ServletWebServerFactory错记录等相关内容,可以在本站寻找。

本文标签: