1 @Component 2 public class WebSocketHandlerMessage implements WebSocketHandler { 3 4 @Autowired 5 private BarrageMessageService barrageMessageService; 6 7 }
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。
解決方法:
方案一:在新建立連接的時候重新從Spring 容器中獲取 BarrageMessageService 對象,這樣就可以正常使用了。
1 @Component 2 public class WebSocketHandlerMessage implements WebSocketHandler { 3 4 /** 5 * 獲取 barrageMessageService 對象方法 6 * 7 * @return 8 */ 9 public BarrageMessageService getMessageService() { 10 return SpringContext.getBean(BarrageMessageService.class); 11 } 12 13 /** 14 * 獲取 stringRedisTemplate 對象方法 15 * 16 * @return 17 */ 18 public StringRedisTemplate getStringRedisTemplate() { 19 return SpringContext.getBean(StringRedisTemplate.class); 20 } 21 }
SpringContext 工具類方法:
1 /** 2 * @Description: SpringContext 獲取 Spring 上下文信息 3 * @Author: mingtian 4 * @CreateDate: 2020/6/8 14:59 5 * @Version: 1.0 6 */ 7 @Component 8 public class SpringContext implements ApplicationContextAware { 9 10 /** 11 * 打印日志 12 */ 13 private Logger logger = LoggerFactory.getLogger(getClass()); 14 15 /** 16 * 獲取上下文對象 17 */ 18 private static ApplicationContext applicationContext; 19 20 21 @Override 22 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 23 SpringContext.applicationContext = applicationContext; 24 logger.info("set applicationContext"); 25 } 26 27 /** 28 * 獲取 applicationContext 29 * 30 * @return 31 */ 32 public static ApplicationContext getApplicationContext() { 33 return applicationContext; 34 } 35 36 /** 37 * 通過 name 獲取 bean 對象 38 * 39 * @param name 40 * @return 41 */ 42 public static Object getBean(String name) { 43 44 return getApplicationContext().getBean(name); 45 } 46 47 /** 48 * 通過 class 獲取 bean 對象 49 * 50 * @param clazz 51 * @param <T> 52 * @return 53 */ 54 public static <T> T getBean(Class<T> clazz) { 55 return getApplicationContext().getBean(clazz); 56 } 57 58 /** 59 * 通過 name,clazz 獲取指定的 bean 對象 60 * 61 * @param name 62 * @param clazz 63 * @param <T> 64 * @return 65 */ 66 public static <T> T getBean(String name, Class<T> clazz) { 67 return getApplicationContext().getBean(name, clazz); 68 } 69 70 }
方案二:使用靜態,讓 service 屬於類,然后給類的 service 注入
1 @Component 2 public class WebSocketHandlerMessage implements WebSocketHandler { 3 4 /** 5 * 這里使用靜態,讓 service 屬於類 6 */ 7 private static BarrageMessageService barrageMessageService; 8 9 /** 10 * 注入的時候,給類的 service 注入 11 */ 12 @Autowired 13 public void setBarrageMessageService(BarrageMessageService barrageMessageService) { 14 WebSocketHandlerMessage.barrageMessageService = barrageMessageService; 15 } 16 }
以上二種方案都可以解決,webSocket 中 service 注入為 null 的問題。