开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务,

定时任务定时查库,相应前台

但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导致的

于是我尝试不用@Autowired 注入实例,自己new ,但是还是失败了,报空指针

这是spring的一个Bug ,需要手动去配置一个类,主动获取实例,在定时任务中(继承TimerTask类),@Autowired 是失效的,无法注入

解决方案如下:

1.首先添加一个工具类,就是application

应注意,同样需要注入添加 @Compent注解

  1. package com.cskaoyan.carparking.utils;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.ApplicationContextAware;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.stereotype.Component;
  8.  
  9. /**
  10. * @Auther: YangTao
  11. * @Date: 2019/2/21 0021
  12. * 配置类,解决定时任务无法注入的问题
  13. */
  14. @Component
  15. public class ApplicationContextUtil implements ApplicationContextAware {
  16.  
  17. private static ApplicationContext applicationContext;
  18.  
  19. public static ApplicationContext getApplicationContext() {
  20. return applicationContext;
  21. }
  22.  
  23. @Override
  24. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  25. ApplicationContextUtil.applicationContext = applicationContext;
  26.  
  27. }
  28.  
  29. public static Object getBean(String beanName) {
  30. return applicationContext.getBean(beanName);
  31. }
  32.  

2.在我们的servcie的实现类注解添加名字,以便我们获取

3.我们在需要的定时任务类中获取service实例就可以使用了

  1. StopService stopService = (StopService) ApplicationContextUtil.getBean("myService");

  

Springboot 定时任务,service层无法注入问题详细解决的更多相关文章

  1. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

  2. quartz整合spring框架service层对象注入为null解决方案

    Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...

  3. 解决service层无法注入

    练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...

  4. springboot测试service层的单元测试

    package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import o ...

  5. SpringBoot:Service层使用@Autowired 注解 mapper对象爆红问题

    问题点 这个报错可能导致程序运行大面积爆红 这个报错会逼疯强迫症 解决方法 为避免程序运行报错 ,需要在Application.class添加注解@MapperScan(mapper包位置) @Spr ...

  6. 如何跳出springboot的service层中某一个方法?

    有一个需求,就是中断某个方法中的for循环 目前的做法是:for循环中,增加if判断,如果满足条件就return,会中断这个方法 for (int i = 0; i < totalIndex; ...

  7. springboot 注册dao层 service 层

    可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...

  8. springboot Service层单元测试

    两个实现类实现同一个Service接口 public interface CustomUrlService { List<ShopMetrics> getShopMetrics(); } ...

  9. SpringBoot中Service实现类添加@Service却任然无法注入的问题

    最近一直在研究Spring Boot.从GitHub上下载了一个my-Blog源码,一边看,一边自己尝试去实现,结果掉在坑了,研究了近一周才爬出来,特地来这博客园记录下来,一是避免自己在放这样的错误, ...

随机推荐

  1. docker 中安装 rabbitMQ

    安装rabbitMQ的命令 docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RAB ...

  2. python学习之可视化tkinter基础

  3. python3.7导入gevent模块报错的解决方案

    最近更新了python解释器3.7 结果安装gevent,在导入gevent之后就报错了,错误信息如下 RuntimeWarning: greenlet.greenlet size changed, ...

  4. 可迭代对象 TO 迭代器

    可迭代对象并不是迭代器,只是支持迭代.可被for循环遍历的对象,比如list,dict ,tuple ,string都是可迭代对象 那既然支持迭代,那要如何用迭代替换for循环呢? 内置函数 iter ...

  5. 一个空格引起的错误。 python

    'render_field' tag requires a form field followed by a list of attributes and values in the form att ...

  6. 多线程之interrupt

    1.interrupt()作为中断程序,并不会直接终止运行,而是设置中断状态,由线程自己处理中断.可以选择终止线程.等待新任务或继续执行. 2.interrupt()经常用于中断处于堵塞状态的的线程, ...

  7. eclipse常用工具

    Eclipse 保存文件时自动格式化代码 很多同学不知道Eclipse有个很有用的功能,就是自动格式源代码的功能,一般大家都是直接Ctrl+Shift+F手动格式化,多浪费时间. 其实Eclipse里 ...

  8. Qt UI tips

    窗口居中:  adjustSize(); move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - h ...

  9. AngelToken——富有价值的区块链服务平台

    关于我们 Angel Token,简称ANG,是基于ETH代币的去中心化数字交易平台. 行业现状 截至2017年12月,全球数字货币总市值已经触及6000亿美元.而2016年12月31日,这个数字才仅 ...

  10. EVE-NG简单入门介绍

    此篇文章简单的介绍下模拟器EVE-NG的使用,具体包括Dynamips设备导入与运行,IOL设备的导入与运行,QEMU设备的导入与运行,客户端软件的安装,物理网络与虚拟网络的结合等. 一.导入镜像 D ...