SpringBoot在WebSocket中注入Service对象
1、在WebSocketServer中将要注入的Service声明成静态变量
@ServerEndpoint("/imserver/{deviceId}") @Component @Slf4j public class WebSocketServer { public static SenderService senderService; }
2、在WebSocketConfig中注入Service
@Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } @Autowired public void setSenderService(SenderService senderService){ WebSocketServer.senderService = senderService; } }