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 ...
随机推荐
- 红楼梦人物关系图,一代大师成绝响,下回分解待何人,kindle读书摘要
人物关系图: https://www.cnblogs.com/images/cnblogs_com/elesos/1120632/o_2033091006.jpg 红楼梦 (古典名著普及文库) ( ...
- 史上最全 Css Hack汇总
前言 每次要找个东西都得慢慢去翻自己收集的一些东西,每次都是那么花时间,再加上有时存放时间久远就忘了当时是存在哪了,为了方便查询及阅读,决定把一些Css Hack 收集起来... 1.区别不同浏览器, ...
- 【HBase】zookeeper在HBase中的应用
转自:http://support.huawei.com/ecommunity/bbs/10242721.html Zookeeper在HBase中的应用 HBase部署相对是一个较大的动作,其依赖于 ...
- on SDN
sdn (software defined network ) emulex 网络 新型网络创新架构 网络虚拟化的一种实现方式 核心技术:OpenFlow 分离网络设备的控制层面和数据层面 目的:实现 ...
- Tensorflow不显示log
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
- unity-------------------打包BuildAssetBundles的使用
unity5打包机制下,一种资源打ab和资源管理的方案.1.打ab: 1.设置平台 2.清楚所有资源的assetbundlename: string[] abNameArr = AssetDataba ...
- (原)使用android studio ndk开发流程
先使用android stuido创建一个app工程,创建工程的时候,.gradle目录结构下为2.8目录.(note:2.10目录为后续更新结果出现.) 依次修改上述红色方框标注部分内容: 1)修改 ...
- 基于SOA的组件化业务基础平台[转]
转自https://www.ibm.com/developerworks/cn/webservices/1111_xiaojg_soa/index.html 业务基础平台是业务逻辑和基础架构平台之间的 ...
- 3d md5 demo
描述:场景3dmax做的,随便拖的几个东西 模型玩过游戏的都知道是doom3的怪兽猪脚 音频是openal播放的wav文件 下载地址:http://pan.baidu.com/s/1eQ8SYI2
- Struts2/XWork 安全漏洞及解决办法
exploit-db网站在7月14日爆出了一个Struts2的远程执行任意代码的漏洞. 漏洞名称:Struts2/XWork < 2.2.0 Remote Command Execution V ...