【发布时间】:2018-10-11 11:57:02
【问题描述】:
我知道关于这个话题有很多问题。我已经阅读了 spring boot doc 和这里的所有解决方案。根据 spring boot doc,@ServerEndpoint 是 Javax 注释,@Autowired 组件由 spring-boot 管理。这两个不能一起使用。对此的解决方案是将SpringConfigurator 添加为ServerEndpoint 的配置器。当我尝试这样做时,我确实收到以下错误:
未能找到根 WebApplicationContext。没有使用 ContextLoaderListener 吗?
spring-boot websocketpage中没有使用ContextLoaderListener的例子。如何使用ContextLoaderListener,以便将组件注入到@ServerEndpoint注解的控制器中?
以下是我的代码。
Websocket 控制器
@ServerEndpoint(value = "/call-stream", configurator = SpringConfigurator.class)
public class CallStreamWebSocketController
{
@Autowired
private IntelligentResponseService responseServiceFacade;
// Other methods
}
Websocket 配置
@Configuration
public class WebSocketConfiguration
{
@Bean
public CallStreamWebSocketController callStreamWebSocketController()
{
return new CallStreamWebSocketController();
}
@Bean
public ServerEndpointExporter serverEndpointExporter()
{
return new ServerEndpointExporter();
}
}
编辑:
这已被标记为 this 问题的副本。我已经尝试了答案中指定的解决方案。解决方案是将SpringConfigurator 添加为@ServerEndpoint 的配置器。添加后,我仍然会收到详细信息中提到的错误。
【问题讨论】:
-
我已经提到了这个问题。它说(就像 Spring Boot 文档一样)将
SpringConfigurator添加为@ServerEndpoint配置器。正如我在问题中明确表示的那样,这是行不通的。 -
你提到的spring-websocket依赖添加到classpath了吗?
-
我使用的是 spring-boot 而不是 spring。所以我将
org.springframework.boot:spring-boot-starter-websocket添加到我的类路径中。
标签: java spring-boot autowired java-websocket