spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误

spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入)。

解决方法:将要注入的 service 改成 static,就不会为null了。
参考代码:

@Controller
@ServerEndpoint(value="/chatSocket")
public class ChatSocket {
// 这里使用静态,让 service 属于类
private static ChatService chatService;

// 注入的时候,给类的 service 注入
@Autowired
public void setChatService(ChatService chatService) {
ChatSocket.chatService = chatService;
}
}

本质原因: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注入一次后,后面的对象就不会再注入了,会报null。

原文地址:https://www.cnblogs.com/xiaojf/p/11137936.html

时间: 2024-08-29 14:32:03

spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误的相关文章

spring 的 ApplicationContext.getBean(type) 无法获取bean,报错

具体问题请看   https://q.cnblogs.com/q/108101/ 研究了两天: 经过上文中的排除法: 造成问题的原因是要获取的bean 中 有被切入的方法.. 就是可能该类会使用反射生成一个类.. 怎么测试呢? 想到 @Autowired  和 @Resource  这两个注解.. 他们会通过 类型 和 名称去找容器中对应 的 bean .. 于是在controller 中使用 这个注解 注入 zaService; 报错了  : Caused by: org.springfram

VSCode------.net core2.0发布后配置到Window Service 2008R2报500错误

如图: 解决方法: 出现这个错误是因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改. 要取消锁定可以运行命令行 %windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers 其中的 handlers 是错误信息中红字显示的节点名称. 如果modules也被锁定,可以运行%windir%\system32\inetsrv\appcmd unlock

springboot的依赖注入报null的问题

最近使用springboot开发项目,使用到了依赖注入,频繁的碰到注入的对象报空指针,错误如下 java.lang.NullPointerException: null at com.mayihc.audit.controller.MaterialNkDetailController.download(MaterialNkDetailController.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

springboot中如果使用了@Autowired注入了bean,则这个类也要为spring bean,new出来注入的bean为null

https://blog.csdn.net/Mr_Runner/article/details/83684088 问题:new出来的实例中含有@Autowired注入时,注入的Bean为null: 解决方法:不要用new的方式实例化,也采用注解的方式,在需要new的实例类上加@Component注解,通过注入的方式使用实例化类: 原因:@Autowired注入时是将类交给Springboot管理,而new出来的实例脱离了Springboot的管理,两个东西不在一个管理者管理下,所以没法联系在一起

Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

URL:http://www.ulewo.com/user/10001/blog/273 我们在使用spring的时候经常会用到这些注解,那么这些注解到底有什么区别呢.我们先来看代码 同样分三层来看: Action 层: package com.ulewo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Co

企业分布式微服务云SpringCloud SpringBoot mybatis (十八)springboot在启动时注入了哪些bean

在程序入口加入: @SpringBootApplication public class SpringbootFirstApplication { public static void main(String[] args) { SpringApplication.run(SpringbootFirstApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ct

关于工具类静态方法调用@Autowired注入的service类问题

@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzName = NtClient.class.getName(); 6 /** 7 * 此处是要使用的service需要spring注入(1) 8 */ 9 @Autowired 10 private NotifyTimeService notifyTimeService; 11 private stat

springboot拦截器注入bean失败实例

如题,在spring拦截器中注入了一个service,结果运行时报了空指针: 2020-01-03 09:26:33.646 |-DEBUG [http-nio-9988-exec-3] org.springframework.web.servlet.DispatcherServlet [1100] -| Failed to complete request: java.lang.NullPointerException 2020-01-03 09:26:33.648 |-ERROR [http

Spring容器启动后注入service到Servlet并自动执行

通常做法是定义一个Servlet,并在web.xml中配置Servlet的启动顺序<load-on-startup>的值在DispatcherServlet之后.但这样做的缺点是在Servlet中无法使用Spring的依赖注入功能,只能使用WebApplicationContext的getBean()方法获取bean. 找到的解决办法如下: 1.自定义一个用于代理启动Servlet的类DelegatingServletProxy: package cn.edu.swu.oa.common.ut