Spring整合webSocket无法注入mapper,service的解决办法

一、为什么webSocket无法注入mapper,service

本质原因: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注入一次后,后面的对象就不会再注入了,会报null。

二、解决无法注入的问题

2.1:修改WebSocketServer

import cloud.zxy.system.dao.SysUserLoginLogMapper;
import cloud.zxy.system.entity.SysUserLoginLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.time.LocalDateTime;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 风行烈
 * @version 1.0
 * @date 2020/9/2 10:10
 */
@Slf4j
@ServerEndpoint("/webSocket/{logId}")
@Component
public class WebSocketServer {


    /**
     * 用来解决webSocket中无法注入mapper
     */
    private static ApplicationContext applicationContext;

	
    public static void setApplicationContext(ApplicationContext applicationContext) {
        WebSocketServer.applicationContext = applicationContext;
    }


    //concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
    private static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();

    //建立连接成功调用
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "logId") String loginId){
        sessionPools.put(loginId, session);
        log.info("欢迎你登录此系统{},你当前登录的时间为{}",loginId,LocalDateTime.now());
    }

    //关闭连接时调用
    @OnClose
    public void onClose(@PathParam(value = "logId") String loginId){
        sessionPools.remove(loginId);
        log.info("你退出此系统{},你当前退出的时间为{}",loginId,LocalDateTime.now());

        SysUserLoginLogMapper sysUserLoginLogMapper = applicationContext.getBean(SysUserLoginLogMapper.class);

        //获取数据库的登录时间 然后根据退出的时间计算出
        SysUserLoginLog userLoginLog = sysUserLoginLogMapper.selectById(loginId);
        //获取登录的时间
        Long startTime = userLoginLog.getLoginTime().getTime();
        //获取退出的时间
        Long endTime = System.currentTimeMillis();
        //退出的时间 减去 登录的时间 求出来的是当前登录的时间 (当前求出来的是毫秒,需要将毫秒转成分钟)
        Integer log = Math.toIntExact(endTime - startTime);
        int loginTime = (log / 1000) / 60;
        userLoginLog.setPeriod(loginTime);
        //持久到数据库中
        sysUserLoginLogMapper.updateById(userLoginLog);
    }

    //错误时调用
    @OnError
    public void onError(Session session, Throwable throwable){
        System.out.println("发生错误");
        throwable.printStackTrace();
    }

2.2:修改启动类

public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(AuthApplication.class);
        ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
        //解决WebSocket不能注入的问题
        WebSocketServer.setApplicationContext(configurableApplicationContext);
    }

你可能感兴趣的:(websocket,websocket)