WebSocket注入Bean后报null,操作不了数据库的解决方法
来源:互联网 发布:笑傲江湖决赛知乎 编辑:程序博客网 时间:2024/06/05 00:18
注入Service访问时报null,多番查询终于得以解决
我有个需求是微信小程序要通过WebSocket获取数据传到后端,进而操作数据库,给MyWebSocket处理类加了@Controller注解,通过@Resource注解注入Service,结果访问时报null,网上搜了很多,花了很长时才搞定,写此博文,即搜即用,经过整理,现附上解决方法(我用的Springboot框架):
- 新建SpringUtil
- 修改启动类App.java
- 修改MyWebSocket处理类
第一步
//新建SpringUtil用来获取beanimport org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class SpringUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } System.out.println("---------------simple.plugin.spring.SpringUtil------------------------------------------------------"); System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========"); } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通过class获取Bean. public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); }}
第二步
//修改启动类App.javaimport org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Import;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@ServletComponentScan@RestController@Import(value={SpringUtil.class}) //引入SpringUtilpublic class App { /**注册Spring Util * 这里为了和上一个冲突,所以方面名为:springUtil2 * 实际中使用springUtil */ @Bean public SpringUtil springUtil2(){ return new SpringUtil(); } public static void main(String[] args) { SpringApplication.run(App.class, args); }}
第三步通过SpringUtil.getBean(BeanName)获取bean,这里的BeanName即@service(“BeanName”),注意需要强转为你的service类的类型
//修改MyWebSocket处理类 @OnMessage public void onMessage (String message, Session session) throws IOException { System.out.println("来自客户端的消息:" + message); String id = message; //获取bean,"vodStaService"为service的Name vodStaService service = (vodStaService) SpringUtil.getBean("vodStaService"); //更新 service.updateByVdid(id); //查询 vodSta selectedNum = service.selectByVdid(id); String number = selectedNum.getSelectedNum(); // 群发消息 for ( MyWebSocket item : webSocketSet ){ item.sendMessage(number); } }
好了,至此,这个问题完美解决,谢谢大神们的分享,有问题请留言~
这里导入bean的方法参考于大神林祥纤的博客 —— [ Spring Boot普通类调用bean ]
0 0
- WebSocket注入Bean后报null,操作不了数据库的解决方法
- ssh 注入了bean 但是取值为null的解决方法!
- 解决spring boot websocket无法注入bean的问题
- 解决spring boot websocket无法注入bean的问题
- 静态变量注入bean的解决方法
- 安装JabRef后运行不了的解决方法
- Spring bean注入为null
- 进入em后,报错:java.lang.Exception: Exception in sending Request : null的解决方法
- springboot websocket 解决不能注入bean问题
- 误操作,将数据文件物理删除,访问不了数据库,解决方法
- oracle安装后 tomcat启动报 Address already in use: JVM_Bind<null>:8080 错误 的几种解决方法
- SSH下Action中无法注入业务bean的解决方法
- SpringBoot中Junit测试注入Bean失败的解决方法
- JBuilder启动不了或者报错的解决方法
- spring给Bean属性注入null值
- Spring 给Bean属性注入null值
- Spring如何为bean注入null值
- springcloud feign 注入bean null问题
- Java异常简介及其架构
- java面试题(六)
- 容易被误解的overflow:hidden
- 【J2SE】——面向对象
- 搭建JavaWeb开发环境使用IDEA开发工具
- WebSocket注入Bean后报null,操作不了数据库的解决方法
- java面试题(七)
- bzoj 1453 双面棋盘 LCT 并查集
- lucene的代码示范
- poj3268
- Adobe PhotoShop CS6 for Mac(苹果)中文破解版下载及破解方法
- 2016年12月24号网络文摘阅读笔记
- 汇编语言编译工具DUBUG使用与安装
- C#学习笔记1