开发一个微信小程序后台,建立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层无法注入问题详细解决的更多相关文章

  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. for in 中的index

    自己学艺不精... 数据是个对象,想要里面的key 原来 for (item, index) in items 中的index 就是key

  2. edu9E. Thief in a Shop

    题意:n个物品每个价值a[i],要求选k个,可以重复,问能取到哪几个价值 题解:fft裸题.但是直接一次fft,然后快速幂会boom.这样是严格的\(2^{20}*log2(2^{20})*log(w ...

  3. exe所在路径

    procedure TForm1.btn1Click(Sender: TObject); begin //完整路径 E:\UsrSoft\Desktop\FileNameDemo\Project1.e ...

  4. 微信小程序笔记

    1.文件的作用 js,wxml,wxss,json 所有页面中要用到的变量,都放在可了pages目录下 wxml:类似于html文件 wxss:类似于css文件(类, id, 标签,子代,后代,bef ...

  5. Hive - - 分组求最大,最小(加行键)

    Hive - - 分组求最大,最小(加行键) 数据: 1325927 陕西 汉中 084 08491325928 陕西 汉中 084 08491325930 陕西 延安 084 08421325931 ...

  6. uva 10288 gailv

    Problem F Coupons Input: standard input Output: standard output Time Limit: seconds Memory Limit: MB ...

  7. 八大排序算法——堆排序(动图演示 思路分析 实例代码java 复杂度分析)

    一.动图演示 二.思路分析 先来了解下堆的相关概念:堆是具有以下性质的完全二叉树:每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆:或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆.如 ...

  8. 错误:Could not find a getter for CreatTime in class

    org.hibernate.PropertyNotFoundException: Could not find a getter for CreatTime in class org.com.xk.h ...

  9. vBox Arch UEFI LVM安装

    Table of Contents 介绍 配置 基础 VirtualBox配置 安装准备 基础 分区 格式化 挂载 安装 选择镜像 安装基本系统 配置 fstab chroot 一些配置 lvm2 网 ...

  10. python简单爬虫 用beautifulsoup爬取百度百科词条

    目标:爬取“湖南大学”百科词条并处理数据 需要获取的数据: 源代码: <div class="basic-info cmn-clearfix"> <dl clas ...