1.在启动类中向 WebSocketServer 中注入 ApplicationContext(WebSocket服务已集成,若未集成,可参考SpringBoot 超简单集成 WebSocket)
@SpringBootApplication
public class WebSocketApplication {
public static void main(String[] args) {
SpringApplication sa= new SpringApplication(WebSocketApplication .class);
ConfigurableApplicationContext cac= sa.run(args);
WebSocketServer.setApplicationContext(cac);
}
}
2.在 WebSocketServer 中通过 ApplicationContext.getBean() 获取 bean
public static void setApplicationContext(ApplicationContext applicationContext) {
WebSocketServer.applicationContext = applicationContext;
}
3.注入 Mapper
DemoMapper demoMapper = applicationContext.getBean(DemoMapper.class);
4.完整 WebSocketServer 示例代码
package com.ymw.system.service.websocket;
import com.baomidou.mybatisplus.core.conditions.query.