场景
SpringBoot整合WebSocket时调用service和mapper的方法:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/114829426
上面讲了在集成websocket后,调用mapper和service的方法。
如果直接在WebSocketServer中通过@Autowired注入mapper,会报空
下面具体细化应用示例。
原因:
项目启动时初始化,会初始化 websocket (非用户连接的),spring 同时会为其注入 service,该对象的 service 不是 null,被成功注入。但是,由于 spring 默认管理的是单例,所以只会注入一次 service。
当新用户进入聊天时,系统又会创建一个新的 websocket 对象,这时矛盾出现了:spring
管理的都是单例,不会给第二个 websocket 对象注入 service,所以导致只要是用户连接创建的 websocket 对象,都不能再注入了。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、在启动类中向WebSocketServer中注入ApplicationContext
@SpringBootApplication public class ImserverApplication { public static void main(String[] args) { //修改前 //SpringApplication.run(ImserverApplication.class, args); //修改后-- //解决websocketServer无法注入mapper问题 SpringApplication springApplication = new SpringApplication(ImserverApplication.class); ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args); WebSocketServer.setApplicationContext(configurableApplicationContext); } }
2、在WebSocketServer中通过ApplicationContext.getBean()获取bean
private static ApplicationContext applicationContext; private ChatRoomMessageMapper chatRoomMessageMapper; //解决无法注入mapper问题 public static void setApplicationContext(ApplicationContext applicationContext) { WebSocketServer.applicationContext = applicationContext; }
这里的chatRoomMessageMapper就是要注入的mapper
调用mapper的方法
//实例化bean chatRoomMessageMapper = applicationContext.getBean(ChatRoomMessageMapper.class); chatRoomMessageMapper.insertChatRoomMessage(messageDeserialize);
完整WebSocketServer示例代码
package com.chrisf.websocket; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.chrisf.domain.ChatRoomMessage; import com.chrisf.enums.ChatRoomMessageTypeEnum; import com.chrisf.enums.ImTypeUEnum; import com.chrisf.imextend.ImUsers; import com.chrisf.mapper.ChatRoomMessageMapper; import com.chrisf.sdk.protocal.Protocal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.util.Date; import java.util.concurrent.Semaphore; /** * websocket 消息处理 * * @author ruoyi */ @Component @ServerEndpoint("/chatroom/websocket/message") public class WebSocketServer { private static ApplicationContext applicationContext; private ChatRoomMessageMapper chatRoomMessageMapper; //解决无法注入mapper问题 public static void setApplicationContext(ApplicationContext applicationContext) { WebSocketServer.applicationContext = applicationContext; } /** * WebSocketServer 日志控制器 */ private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class); /** * 默认最多允许同时在线人数100 */ public static int socketMaxOnlineCount = 100; private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount); /** * 连接建立成功调用的方法 */ @OnOpen public void onOpen(Session session) throws Exception { boolean semaphoreFlag = false; // 尝试获取信号量 semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore); if (!semaphoreFlag) { // 未获取到信号量 LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount); WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount); session.close(); } else { // 添加用户 WebSocketUsers.put(session.getId(), session); LOGGER.info("\n 建立连接 - {}", session); LOGGER.info("\n 当前人数 - {}", WebSocketUsers.getUsers().size()); WebSocketUsers.sendMessageToUserByText(session, "连接成功"); } } /** * 连接关闭时处理 */ @OnClose public void onClose(Session session) { LOGGER.info("\n 关闭连接 - {}", session); // 移除用户 WebSocketUsers.remove(session.getId()); // 获取到信号量则需释放 SemaphoreUtils.release(socketSemaphore); } /** * 抛出异常时处理 */ @OnError public void one rror(Session session, Throwable exception) throws Exception { if (session.isOpen()) { // 关闭连接 session.close(); } String sessionId = session.getId(); LOGGER.info("\n 连接异常 - {}", sessionId); LOGGER.info("\n 异常信息 - {}", exception); // 移出用户 WebSocketUsers.remove(sessionId); // 获取到信号量则需释放 SemaphoreUtils.release(socketSemaphore); } /** * 服务器接收到客户端消息时调用的方法 */ @OnMessage public void onMessage(String message, Session session) { String msg = message; //解析json数据 ChatRoomMessage messageDeserialize = JSON.parseObject(msg, ChatRoomMessage.class); //为避免各客户端时间不一致,统一后台赋值时间 messageDeserialize.setSendTime(new Date()); //实例化bean chatRoomMessageMapper = applicationContext.getBean(ChatRoomMessageMapper.class); chatRoomMessageMapper.insertChatRoomMessage(messageDeserialize); //格式化时间显示 msg = JSON.toJSONStringWithDateFormat(messageDeserialize,"yyyy-MM-dd:HH:mm:ss", SerializerFeature.WriteDateUseDateFormat); //群发给所有的web端用户 WebSocketUsers.sendMessageToUsersByText(msg); } }