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;
    }

}

 

posted @ 2020-09-25 17:52  孤独二世  阅读(2227)  评论(0编辑  收藏  举报