五)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 ...
随机推荐
- PDO exec 执行时出错后如果修改数据会被还原?
PDO exec 执行时出错后如果修改数据会被还原? 现象 FastAdmin 更新了 1127 版本,但是使用在线安装方式出现无法修改管理员密码的问题. 一直是默认的 admin 123456 密码 ...
- Tcl 和 Raft 发明人的软件设计哲学
John Ousterhout(斯坦福大学教授,Tcl 语言.Raft 协议的发明人...真的是超级牛人,Title 好多好多,这里就列几个大家熟悉的),在 Google 做了一次演讲,题目就叫 「A ...
- Ionic 项目创建
1. Ionic介绍 Ionci 是一个强大的HTML5 应用程序框架. 可以帮助您使用Web技术,如HTML, CSS和Javascript构建原生体验的移动应用程序. Ionic主要关注外观和体验 ...
- laravel 整合 swoole ,并简单 ab 测试对比性能以及在 PHPstorm 中利用debug调试配置swoole服务中的PHP代码
安装PHP 的 swoole 扩展 及 安装 laravel,就不描述了 整合 laravel 和 swoole 用了这个轮子,侵入性很小,一行代码搞定,推荐一下,今天刚用,不能预测未来是否会遇见坑 ...
- debian7配置iptables
vim /etc/iptables.rule 文件内容如下 *filter # Allows all loopback (lo0) traffic and drop all traffic to / ...
- Windows Server 2012/win8 iis8 上安装 asp.net 4.5 当时用了mvc5 .net framework 4.5 所以得装下
vs2013+mvc5 +.net framework 4.5 本地 iisexpress 调试一点问题没有,当部署到本机iis时 出现 无法识别 modules错误,具体错误提示是: 锁定是默认设 ...
- Express详解
express() 创建一个express应用程序 var express = require('express'); var app = express(); app.get('/', functi ...
- C# CS1591 缺少对公共可见类型或成员的 XML 注释 问题解决
最近在写web api的项目,用到微软的Web api help page组件,用于自动对生成API文档,见博文: https://www.cnblogs.com/lenmom/p/9081363.h ...
- centos6挂载U盘
一.FAT格式的U盘 插入U盘 [root@localhost ~]# dmesg | grep usb usbcore: registered new interface driver usbfs ...
- Spring batch学习 (1)
Spring Batch 批处理框架 埃森哲和Spring Source研发 主要解决批处理数据的问题,包含并行处理,事务处理机制等.具有健壮性 可扩展,和自带的监控功能,并且支持断点和重发.让程序员 ...