代码示例:
@Component
public class WebSocketHandlerMessage implements WebSocketHandler {
@Autowired
private BarrageMessageService barrageMessageService;
}
结果显示:
SpringBoot项目集成 webSocket,当客户端与服务器端建立连接的时候,发现 barrageMessageService 对象并未注入而是为 null。
产生原因:spring管理的都是单例(singleton),和 websocket (多对象)相冲突。
详细解释:项目启动时初始化,会初始化 websocket (非用户连接的),spring 同时会为其注入 service,该对象的 service 不是 null,被成功注入。但是,由于 spring 默认管理的是单例,所以只会注入一次 service。当客户端与服务器端进行连接时,服务器端又会创建一个新的 websocket 对象,这时问题出现了:spring 管理的都是单例,不会给第二个 websocket 对象注入 service,所以导致只要是用户连接创建的 websocket 对象,都不能再注入了。
像 controller 里面有 service, service 里面有 dao。因为 controller,service ,dao 都有是单例,所以注入时不会报 null。但是 websocket 不是单例,所以使用spring注入一次后,后面的对象就不会再注入了,会报NullException。
解决方法:
方案一:使用静态,让 service 属于类,然后给类的 service 注入。
@Component
public class WebSocketHandlerMessage implements WebSocketHandler {
/**
* 这里使用静态,让 service 属于类
*/
private static BarrageMessageService barrageMessageService;
/**
* 注入的时候,给类的 service 注入
*/
@Autowired
public void setBarrageMessageService(BarrageMessageService barrageMessageService) {
WebSocketHandlerMessage.barrageMessageService = barrageMessageService;
}
}
方案二:在新建立连接的时候重新从Spring 容器中获取 BarrageMessageService 对象,这样就可以正常使用了。
@Component
public class WebSocketHandlerMessage implements WebSocketHandler {
/**
* 获取 barrageMessageService 对象方法
*
* @return
*/
public BarrageMessageService getMessageService() {
return SpringContext.getBean(BarrageMessageService.class);
}
/**
* 获取 stringRedisTemplate 对象方法
*
* @return
*/
public StringRedisTemplate getStringRedisTemplate() {
return SpringContext.getBean(StringRedisTemplate.class);
}
}
SpringContext 工具类方法:
/**
* @Description: SpringContext 获取 Spring 上下文信息
* @Author: mingtian
* @CreateDate: 2020/6/8 14:59
* @Version: 1.0
*/
@Component
public class SpringContext implements ApplicationContextAware {
/**
* 打印日志
*/
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 获取上下文对象
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContext.applicationContext = applicationContext;
logger.info("set applicationContext");
}
/**
* 获取 applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过 name 获取 bean 对象
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过 class 获取 bean 对象
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过 name,clazz 获取指定的 bean 对象
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
以上二种方案都可以解决,webSocket 中 service 注入为 null 的问题。