webSocket 中使用 @Autowired 注入对应为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 对象,这样就可以正常使用了。
@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
    • @return
      */
      public static T getBean(Class clazz) {
      return getApplicationContext().getBean(clazz);
      }

    /**

    • 通过 name,clazz 获取指定的 bean 对象
    • @param name
    • @param clazz
    • @param
    • @return
      */
      public static T getBean(String name, Class clazz) {
      return getApplicationContext().getBean(name, clazz);
      }

}
方案二:使用静态,让 service 属于类,然后给类的 service 注入
@Component
public class WebSocketHandlerMessage implements WebSocketHandler {

/**
 *  这里使用静态,让 service 属于类
 */
private static BarrageMessageService barrageMessageService;

/**
 *  注入的时候,给类的 service 注入
 */
@Autowired
public void setBarrageMessageService(BarrageMessageService barrageMessageService) {
    WebSocketHandlerMessage.barrageMessageService = barrageMessageService;
}

引入redis资源
@Resource
private RedisCache RedisCache = SpringUtils.getBean(RedisCache.class);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在您提供的代码,`userTableService` 对象为空,可能是因为使用了错误的注解或者注入对象的时机不正确。 1. 错误的注解:`@Autowired` 注解是 Spring 常用的注解,用于自动注入依赖对象。在使用 `@Autowired` 注解时,需要确保注入对象的类被正确标记为 Spring 组件,可以使用 `@Component`、`@Service`、`@Repository` 等注解标记类。如果没有标记类,则注入对象会失败,导致对象为空。 2. 注入对象的时机不正确:在使用 `@Autowired` 注解时,需要确保注入对象的时机正确。如果在类的静态成员或者静态方法使用 `@Autowired` 注解来注入对象,就会导致注入对象失败,导致对象为空。需要在类的实例成员或者实例方法使用 `@Autowired` 注解来注入对象。 针对您提供的代码,可以尝试以下解决方案: 1. 在 `IUserTableService` 的实现类中添加 Spring 组件注解,例如 `@Service` 注解。 2. 将 `userTableService` 的修饰符由 `private static` 改为 `private`,并将使用 `@Autowired` 注解的地方移到实例方法,确保在类的实例化后再进行注入。 ```java @Autowired private IUserTableService userTableService; @OnClose public void onClose() { // 使用 userTableService 对象 } ``` 3. 在当前类中添加构造函数,并使用 `@Autowired` 注解来注入 `userTableService` 对象。 ```java private IUserTableService userTableService; @Autowired public WebSocketServer(IUserTableService userTableService) { this.userTableService = userTableService; } @OnClose public void onClose() { // 使用 userTableService 对象 } ``` 需要注意的是,在使用 `@Autowired` 注解时,需要确保 Spring 容器已经正确初始化,并且 `userTableService` 对象已经被正确注入。可以通过添加日志输出或者调试工具对代码进行调试,找出出现问题的具体位置和原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值