五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="cn.zno" />
<task:executor id="threadPoolTaskExecutor" pool-size="1" /> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="startupDelay" value="0" />
<property name="overwriteExistingJobs" value="true" />
<property name="exposeSchedulerInRepository" value="true" />
<property name="taskExecutor" ref="threadPoolTaskExecutor" />
<property name="triggers">
<list>
<ref bean="cronTrigger_1" />
</list>
</property>
</bean> <bean id="cronTrigger_1"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail_1" />
<property name="cronExpression" value="* * * * * ?" />
</bean>
<bean id="jobDetail_1"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.zno.job.Breathe" />
</bean>
<bean id="breath" class="cn.zno.job.Breathe" />
</beans>
为什么要配置这个
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
因为可以通过这个key取到Spring上下文。
配置1存在的问题:不能自动注入。
package cn.zno.job; import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext; import cn.zno.service.AService; public class Breathe implements Job { // @Autowired
// private AService aService; @Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
try {
// aService.Show();
ApplicationContext ctx = (ApplicationContext)context.getScheduler().getContext().get("applicationContextKey");
AService aService = ctx.getBean(AService.class);
aService.Show();
} catch (Exception e) {
e.printStackTrace();
} } }
配置一改:解决自动注入问题
change
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
to
<property name="jobFactory">
<bean class="cn.zno.common.SpringBeanJobFactory" />
</property>
cn.zno.common.SpringBeanJobFactory
package cn.zno.common; import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class SpringBeanJobFactory extends
org.springframework.scheduling.quartz.SpringBeanJobFactory implements
ApplicationContextAware { private ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext; } @Override
protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
Object jobInstance = super.createJobInstance(bundle);
applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
return jobInstance;
}
}
另一种实现方式为:
package cn.zno.common; import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; public class SpringBeanJobFactory extends
org.springframework.scheduling.quartz.SpringBeanJobFactory { @Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory; @Override
protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
Object jobInstance = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类的更多相关文章
- 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- 普通Java类获取spring 容器的bean的5种方法
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)
Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...
- Spring -08 -自动注入 -byName/byType/constructor -全局使用default-autowire=” byName"
1.在Spring 配置文件中对象名和ref=”id”id 名相同使用自动注入,可以不配置<property/>2.两种配置办法 2.1在<bean>中通过 autowire= ...
- Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
- Spring 注解Autowired自动注入bean异常解决
错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...
- 分析解决 spring quartz 中出现的执行两次问题
1. 问题描述 在开发询盘功能时,遇到一个需求,就是后台定时任务执行用电施工业务的工单下发. 使用的技术是 spring quartz,因为其他应用有先例,配置quartz 完成后,先写了一个 hel ...
随机推荐
- SpookyOTP
https://pypi.python.org/pypi/SpookyOTP/1.1.1 SpookyOTP 1.1.1 Downloads ↓ A lightweight Python 2/3 pa ...
- windows7下安装python环境和django
1.安装python 1.1.首先访问http://www.python.org/download/去下载最新的python版本. 根据计算机位数选择对应的版本比如我的机器是64位的,我就下载这个安装 ...
- UEditor自定义toolbar工具条
使用ueditor的同学都知道,ueditor里有很多功能,很全面,但有时候我们的编辑器不需要太多的功能,比如前台评论或者留言,就不需要这么多功能了,那我们怎么去定制自己想要的工具呢?官方给出了两个方 ...
- mysql负载均衡完美解决方案
http://blog.csdn.net/v1v1wang/article/details/8771974 1.环境: mysql 5ubuntu10.04 x86_64 mdb1 et ...
- laravel Hash密码 校对
laravel加密 是使用hash不可逆的,但是可以对加密后的密码进行校对 $data = $r->all();$id = $data['id'];$user_password = bcrypt ...
- 常用数据结构的功能及复杂度总结(OI)
不定长数组 维护一个序列 在末尾插入/删除均摊O(1) 任意位置插入O(n) 指定位置查询/修改O(1) 空间O(n) 链表 维护一个序列 定位到第i个位置O(n) 在任意位置(已定位到该位置)插入/ ...
- Oracle VM Virtualbox基础知识
修改硬盘的UUID VBoxManage internalcommands sethduuid <filename>
- Centos编译Redis4.0.9源码过程记录
mkdir /home/redis cd /home/redis 下载源码 wget https://codeload.github.com/antirez/redis/tar/4.0.9 解压源码包 ...
- 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree
▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题 ▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ ...
- 17.Letter Combinations of a Phone Number(Back-Track)
Given a digit string, return all possible letter combinations that the number could represent. A map ...