Springboot 定时任务,service层无法注入问题详细解决
开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务,
定时任务定时查库,相应前台
但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导致的
于是我尝试不用@Autowired 注入实例,自己new ,但是还是失败了,报空指针
这是spring的一个Bug ,需要手动去配置一个类,主动获取实例,在定时任务中(继承TimerTask类),@Autowired 是失效的,无法注入
解决方案如下:
1.首先添加一个工具类,就是application
应注意,同样需要注入添加 @Compent注解
- package com.cskaoyan.carparking.utils;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
- /**
- * @Auther: YangTao
- * @Date: 2019/2/21 0021
- * 配置类,解决定时任务无法注入的问题
- */
- @Component
- public class ApplicationContextUtil implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- ApplicationContextUtil.applicationContext = applicationContext;
- }
- public static Object getBean(String beanName) {
- return applicationContext.getBean(beanName);
- }
2.在我们的servcie的实现类注解添加名字,以便我们获取
3.我们在需要的定时任务类中获取service实例就可以使用了
- StopService stopService = (StopService) ApplicationContextUtil.getBean("myService");
Springboot 定时任务,service层无法注入问题详细解决的更多相关文章
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
- quartz整合spring框架service层对象注入为null解决方案
Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...
- 解决service层无法注入
练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...
- springboot测试service层的单元测试
package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import o ...
- SpringBoot:Service层使用@Autowired 注解 mapper对象爆红问题
问题点 这个报错可能导致程序运行大面积爆红 这个报错会逼疯强迫症 解决方法 为避免程序运行报错 ,需要在Application.class添加注解@MapperScan(mapper包位置) @Spr ...
- 如何跳出springboot的service层中某一个方法?
有一个需求,就是中断某个方法中的for循环 目前的做法是:for循环中,增加if判断,如果满足条件就return,会中断这个方法 for (int i = 0; i < totalIndex; ...
- springboot 注册dao层 service 层
可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...
- springboot Service层单元测试
两个实现类实现同一个Service接口 public interface CustomUrlService { List<ShopMetrics> getShopMetrics(); } ...
- SpringBoot中Service实现类添加@Service却任然无法注入的问题
最近一直在研究Spring Boot.从GitHub上下载了一个my-Blog源码,一边看,一边自己尝试去实现,结果掉在坑了,研究了近一周才爬出来,特地来这博客园记录下来,一是避免自己在放这样的错误, ...
随机推荐
- docker 中安装 rabbitMQ
安装rabbitMQ的命令 docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RAB ...
- python学习之可视化tkinter基础
- python3.7导入gevent模块报错的解决方案
最近更新了python解释器3.7 结果安装gevent,在导入gevent之后就报错了,错误信息如下 RuntimeWarning: greenlet.greenlet size changed, ...
- 可迭代对象 TO 迭代器
可迭代对象并不是迭代器,只是支持迭代.可被for循环遍历的对象,比如list,dict ,tuple ,string都是可迭代对象 那既然支持迭代,那要如何用迭代替换for循环呢? 内置函数 iter ...
- 一个空格引起的错误。 python
'render_field' tag requires a form field followed by a list of attributes and values in the form att ...
- 多线程之interrupt
1.interrupt()作为中断程序,并不会直接终止运行,而是设置中断状态,由线程自己处理中断.可以选择终止线程.等待新任务或继续执行. 2.interrupt()经常用于中断处于堵塞状态的的线程, ...
- eclipse常用工具
Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclipse里 ...
- Qt UI tips
窗口居中: adjustSize(); move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - h ...
- AngelToken——富有价值的区块链服务平台
关于我们 Angel Token,简称ANG,是基于ETH代币的去中心化数字交易平台. 行业现状 截至2017年12月,全球数字货币总市值已经触及6000亿美元.而2016年12月31日,这个数字才仅 ...
- EVE-NG简单入门介绍
此篇文章简单的介绍下模拟器EVE-NG的使用,具体包括Dynamips设备导入与运行,IOL设备的导入与运行,QEMU设备的导入与运行,客户端软件的安装,物理网络与虚拟网络的结合等. 一.导入镜像 D ...