58人参与 • 2024-10-22 • websocket
代码示例:
@component public class websockethandlermessage implements websockethandler { @autowired private barragemessageservice barragemessageservice; }
结果显示:
springboot项目集成 websocket,当客户端与服务器端建立连接的时候,发现 barragemessageservice 对象并未注入而是为 null。
产生原因:spring管理的都是单例(singleton),和 websocket (多对象)相冲突。
详细解释:项目启动时初始化,会初始化 websocket (非用户连接的),spring 同时会为其注入 service,该对象的 service 不是 null,被成功注入。但是,由于 spring 默认管理的是单例,所以只会注入一次 service。当客户端与服务器端进行连接时,服务器端又会创建一个新的 websocket 对象,这时问题出现了:spring 管理的都是单例,不会给第二个 websocket 对象注入 service,所以导致只要是用户连接创建的 websocket 对象,都不能再注入了。
像 controller 里面有 service, service 里面有 dao。因为 controller,service ,dao 都有是单例,所以注入时不会报 null。但是 websocket 不是单例,所以使用spring注入一次后,后面的对象就不会再注入了,会报nullexception。
解决方法:
方案一:使用静态,让 service 属于类,然后给类的 service 注入。
@component public class websockethandlermessage implements websockethandler { /** * 这里使用静态,让 service 属于类 */ private static barragemessageservice barragemessageservice; /** * 注入的时候,给类的 service 注入 */ @autowired public void setbarragemessageservice(barragemessageservice barragemessageservice) { websockethandlermessage.barragemessageservice = barragemessageservice; } }
方案二:在新建立连接的时候重新从spring 容器中获取 barragemessageservice 对象,这样就可以正常使用了。
@component public class websockethandlermessage implements websockethandler { /** * 获取 barragemessageservice 对象方法 * * @return */ public barragemessageservice getmessageservice() { return springcontext.getbean(barragemessageservice.class); } /** * 获取 stringredistemplate 对象方法 * * @return */ public stringredistemplate getstringredistemplate() { return springcontext.getbean(stringredistemplate.class); } }
springcontext 工具类方法:
/** * @description: springcontext 获取 spring 上下文信息 * @author: mingtian * @createdate: 2020/6/8 14:59 * @version: 1.0 */ @component public class springcontext implements applicationcontextaware { /** * 打印日志 */ private logger logger = loggerfactory.getlogger(getclass()); /** * 获取上下文对象 */ private static applicationcontext applicationcontext; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { springcontext.applicationcontext = applicationcontext; logger.info("set applicationcontext"); } /** * 获取 applicationcontext * * @return */ public static applicationcontext getapplicationcontext() { return applicationcontext; } /** * 通过 name 获取 bean 对象 * * @param name * @return */ public static object getbean(string name) { return getapplicationcontext().getbean(name); } /** * 通过 class 获取 bean 对象 * * @param clazz * @param <t> * @return */ public static <t> t getbean(class<t> clazz) { return getapplicationcontext().getbean(clazz); } /** * 通过 name,clazz 获取指定的 bean 对象 * * @param name * @param clazz * @param <t> * @return */ public static <t> t getbean(string name, class<t> clazz) { return getapplicationcontext().getbean(name, clazz); } }
以上二种方案都可以解决,websocket 中 service 注入为 null 的问题。
到此这篇关于websocket 中使用 @autowired 注入对应为null的文章就介绍到这了,更多相关websocket @autowired 注入为null内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论