Java Spring-boot - 如何将@Autowired 与@ServerEndpoint 一起使用?
Java Spring-boot - How to use @Autowired with @ServerEndpoint?
我知道有很多关于这个话题的问题。我已阅读 spring 引导文档和此处的所有解决方案。根据 spring 引导文档,@ServerEndpoint
是一个 Javax 注释,@Autowired
组件是 spring-引导管理的。这两个不能一起使用。解决方案是添加 SpringConfigurator
作为 ServerEndpoint
的配置器。当我尝试这样做时,我确实收到以下错误:
Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
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();
}
}
编辑:
这已被标记为 问题的副本。我已经尝试了答案中指定的解决方案。解决方案是添加 SpringConfigurator
作为 @ServerEndpoint
的配置器。添加后我仍然得到详细信息中提到的错误。
经过一番研究,我找到了一种强制 spring-boot 将组件注入外部 managed/instantiated class.
的方法
1) 添加一个通用方法到您的 class 扩展 ApplicationContextAware
到 return 一个 bean。
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContext.context = context;
}
public ApplicationContext getApplicationContext() {
return context;
}
// Generic method to return a beanClass
public static <T> T getBean(Class<T> beanClass)
{
return context.getBean(beanClass);
}
}
2) 使用此方法初始化你要注入的class对象
private IntelligentResponseService responseServiceFacade = SpringContext.getBean(IntelligentResponseService.class);
所以在上面的更改之后我的 websocket 控制器看起来像这样
@ServerEndpoint(value = "/call-stream", configurator = SpringConfigurator.class)
public class CallStreamWebSocketController
{
private IntelligentResponseService responseServiceFacade = SpringContext.getBean(IntelligentResponseService.class);
// Other methods
}
我知道有很多关于这个话题的问题。我已阅读 spring 引导文档和此处的所有解决方案。根据 spring 引导文档,@ServerEndpoint
是一个 Javax 注释,@Autowired
组件是 spring-引导管理的。这两个不能一起使用。解决方案是添加 SpringConfigurator
作为 ServerEndpoint
的配置器。当我尝试这样做时,我确实收到以下错误:
Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
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();
}
}
编辑:
这已被标记为 SpringConfigurator
作为 @ServerEndpoint
的配置器。添加后我仍然得到详细信息中提到的错误。
经过一番研究,我找到了一种强制 spring-boot 将组件注入外部 managed/instantiated class.
的方法1) 添加一个通用方法到您的 class 扩展 ApplicationContextAware
到 return 一个 bean。
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContext.context = context;
}
public ApplicationContext getApplicationContext() {
return context;
}
// Generic method to return a beanClass
public static <T> T getBean(Class<T> beanClass)
{
return context.getBean(beanClass);
}
}
2) 使用此方法初始化你要注入的class对象
private IntelligentResponseService responseServiceFacade = SpringContext.getBean(IntelligentResponseService.class);
所以在上面的更改之后我的 websocket 控制器看起来像这样
@ServerEndpoint(value = "/call-stream", configurator = SpringConfigurator.class)
public class CallStreamWebSocketController
{
private IntelligentResponseService responseServiceFacade = SpringContext.getBean(IntelligentResponseService.class);
// Other methods
}