webSocket 中使用 @Autowired 注入对应为null

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 的问题。

 

posted @ 2020-11-06 09:29  明天,你好啊  阅读(2558)  评论(0编辑  收藏  举报