多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

【记录】WebSocket注入失败分析

【记录】WebSocket注入失败分析 现象websocket里无法注入其它Compenent 、Service、Resource、Autowired、Value等单例组件原因spring管理的都是单例singleton和 websocket 多对象相冲突。websocket 不是单例是动态生成的因此无法注入单例组件。像 controller 里面有 service service 里面有 dao。因为 controllerservice dao 都有是单例所以注入时不会报 null。但是 websocket 不是单例所以使用spring注入一次后后面的对象就不会再注入了会报null。解决使用如下方式将获取方式写在外部类即可避免ServerEndpoint(/websocket)ComponentpublicclassWebSocketServer{// 要注入的组件privatestaticSendConfigsendConfig;AutowiredpublicvoidsetSendConfig(SendConfigsendConfig){WebSocketServer.sendConfigsendConfig;}}本文与Autowired/Resource注入失败如何解决相关
返回列表