Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
一、问题描述:
使用Spring整合quartz实现动态任务时,想在job定时任务中使用某个service时,直接通过加注解@Component、@Autowired是不能注入的,获取的对象为Null。如下面的代码:
- @Component
- @PersistJobDataAfterExecution
- @DisallowConcurrentExecution
- public class TicketSalePriceLessThanLowestPriceJob implements Job{
- @Autowired
- private XxxService xxxService;
- }
二、解决方案:
1、新增一个自定义类(CustomJobFactory),继承SpringBeanJobFactory,代码如下:
- import org.quartz.spi.TriggerFiredBundle;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
- import org.springframework.scheduling.quartz.SpringBeanJobFactory;
- public class CustomJobFactory extends SpringBeanJobFactory{
- @Autowired
- private AutowireCapableBeanFactory capableBeanFactory;
- @Override
- protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
- //调用父类的方法
- Object jobInstance = super.createJobInstance(bundle);
- //进行注入
- capableBeanFactory.autowireBean(jobInstance);
- return jobInstance;
- }
- }
2、在spring.xml文件配置CustomJobFactory,如下:
- <bean id="customJobFactory" class="cn.imovie.manage.task.job.CustomJobFactory"></bean>
3、将自定义CustomJobFactory注入到org.springframework.scheduling.quartz.SchedulerFactoryBean,具体如下:
- <property name="jobFactory" ref="customJobFactory"></property>
完整代码如下:
- <!-- 定时任务配置 start -->
- <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
- <property name="overwriteExistingJobs" value="true" />
- <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
- <property name="startupDelay" value="10" />
- <!-- 设置自动启动 -->
- <property name="autoStartup" value="true" />
- <property name="jobFactory" ref="customJobFactory"></property>
- <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
- <property name="configLocation" value="classpath:spring-quartz.properties" />
- </bean>
- <!-- 定时任务配置 end -->
4、然后就可以在Job任务类使用@Autowired注入service。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright 蕃薯耀 2017年9月6日
http://www.cnblogs.com/fanshuyao/
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入的更多相关文章
- 在maven中开发Spring需要的jar依赖
在maven中开发Spring需要的jar依赖 <properties> <spring.version>4.0.6.RELEASE</spring.version> ...
- Maven构建的Spring项目需要哪些依赖?
Maven构建的Spring项目需要哪些依赖? <!-- Spring依赖 --> <!-- 1.Spring核心依赖 --> <dependency> <g ...
- spring boot 整合quartz ,job不能注入的问题
在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring 3整合Quartz 2实现定时任务--转
常规整合 http://www.meiriyouke.net/?p=82 最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之 ...
- Spring整合Quartz
目录[-] 一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路 ...
- Spring Boot整合Quartz实现定时任务表配置
最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...
- Spring 整合 Quartz 实现动态定时任务
复制自:https://www.2cto.com/kf/201605/504659.html 最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能 ...
- Spring Boot 应用系列 6 -- Spring Boot 2 整合Quartz
Quartz是实现定时任务的利器,Quartz主要有四个组成部分,分别是: 1. Job(任务):包含具体的任务逻辑: 2. JobDetail(任务详情):是对Job的一种详情描述: 3. Trig ...
随机推荐
- android开发 系统时间与定时器之间有关系嘛?
如题: android开发 系统时间与定时器之间有关系嘛? 答案:有. 看定时器源码: /* * Schedule a task. */ private void scheduleImpl(Timer ...
- [技术选型] dubbo
分布式服务架构 - 阿里开源项目 简介 Dubbo架构设计详解 Dubbo与Zookeeper.SpringMVC整合和使用(负载均衡.容错)
- Python 类的多态的运用
#类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...
- Check which .NET Framework version is installed
his article will help you to know which .NET Framework version is installed from command line. Check ...
- C++标准转换运算符static_cast
该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性. 中文名 暂无 外文名 static_cast 分 类 强制类型转换 类 型 C++ s ...
- 摄像头驱动OV7725学习笔记连载(二):0V7725 SCCB时序的实现之寄存器配置
上一篇博客主要介绍了OV7725的电气特性以及SCCB接口的时序和输出一帧图像的时序图以及数据的拼接.输出一帧图像与输出时钟PCLK有关. 上图是OV7725实现的整体框架,有点丑.FPGA描述SCC ...
- 【转】Windows 7 API Internet Connection Sharing(ICS) 与 Wireless Hosted Network构建本地AP
原文:http://hi.baidu.com/ritrachiao/item/bf7715e6bb8cb3a0c10d75be [此刻我要大大地记录一下!] 这个折腾了我好几天的Windows 7 A ...
- Linux命令_搜索文件
一.用which命令查找可执行文件的绝对路径 which命令用来查找PATH环境变量中出现的路径下的可执行文件. 二.用whereis命令查找文件 whereis命令通过预先生成的一个文件列表库查找与 ...
- 使用Maven创建Web应用程序项目
用到的技术/工具: Maven 3.3.3 Eclipse 4.3 JDK 8 Spring 4.1.1.RELEASED Tomcat 7 Logback 1.0.13 1. 从Maven模板创建W ...
- SciTE配置信息
超强文本编辑器SciTE配置方法详细实例 转载 2006年12月28日 17:07:00 标签: 文本编辑 / 文档 / 语言 / html / python / api 32800 关于scite文 ...