记录spring springboot websocket 不能注入( @Autowired ) 解决问题的过程
2019-07-29 16:00
3327 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44037376/article/details/97644830
在WebSocket使用@service注解的service类时,启动没有问题,在发送聊天信息的时候,出现异常:java.lang.NullPointException,过程中找到很多的解决方案,但是这些方法都没有解决,会出现其他的一些错误。
最终得以解决,记录此贴以供后面查阅。
第一种方式:在@ServerEndpoint 注解后面添加 configurator = SpringConfigurator.class
如下
@ServerEndpoint(value = "/websocket/{user}", configurator = SpringConfigurator.class) @Controller
这种方式会出现以下错误,从字面意思上来看是ContextLoaderListener没有运行。
java.lang.IllegalStateException: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?
ContextLoaderListener 是spring MVC web.xml的配置文件,跟Spring boot 关系好像不大,就没有深入,换一种方式。
第二种方式:在websock的配置类中配置websock类
如下:
@Configuration public class EndpointConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter(); serverEndpointExporter.setAnnotatedEndpointClasses(WebsocketController.class); return serverEndpointExporter; } }
WebsocketController 是websocket 实现类,依然没有解决。
第三种方式:重写ApplicationContextAware 类
如下:
@Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil .applicationContext == null) { SpringUtil .applicationContext = applicationContext; } } public static ApplicationContext getApplicationContext(){ return applicationContext; } public static Object getBean(String beanName){ return applicationContext.getBean(beanName); } public static <T> T getBean(Class<T> clazz){ return (T)applicationContext.getBean(clazz); } }
然后,通过getBean获取Service方法
private static WeChatService weChatService = SpringUtil .getBean(WeChatService.class);
看上去这个方法很可行啊,启动,报错 …
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'websocketController' defined in file
bean创建失败,正准备攻克这个问题时,无意中发现了另一个解决方法。
第四种方式:初始化Service 类,再通过@Autowired 进行赋值
如下:
private static WeChatService weChatService ; @Autowired public void setWeChatService(WeChatService weChatService){ this.weChatService = weChatService; }
至此问题解决。
但是第三种方式到底可不可行,有待大佬留言!!!
end!
相关文章推荐
- 解决Springboot @Autowired 无法注入问题
- 详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决
- Spring Boot + Java爬虫 + 部署到Linux(四、使用WebSocket实现消息推送,并解决websocket中的autowired问题)
- 在SpringBoot中 @Configuration类中 @Autowired 不能注入的问题
- springboot websocket 解决不能注入bean问题
- 如何解决SpringBoot JpaRepository @Autowired 没法自动注入的问题
- 解决Springboot中@Autowired 无法注入问题
- Spring boot @@Autowired注入空指针问题解决方案
- Spring Boot @Autowired 没法自动注入的问题
- @resource或@Autowired Service service不能通过spring注入问题
- 解决Intellij Idea Spring Boot Mybatis @Autowired报错的问题
- 关于springboot使用@Autowired注解向非controller类中注入bean为null的问题
- spring boot 读取配置文件问题,不能使用@Autowired
- mapper注入失败问题解决 {@org.springframework.beans.factory.annotation.Autowired(required=true)}
- 在搭建spring boot环境时,碰到总是不能Autowired,总是提示需要注入bean文件
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- (二)SpringBoot源码解析--自动注入过程 Autowired原理(1)
- Spring Boot 自定义 Shiro 过滤器无法使用 @Autowired问题及解决方法
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
- SpringBoot学习——@Autowired自动注入报:could not be found问题的理解和解决方案