五)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 ...
随机推荐
- 再看Spring Could微服务的关键组件
Consul是用go开发的开源注册中心服务,内置服务发现与注册.raft一致性协议实现.健康检查.多数据中心等方案.与Eurker相比,consul还能对异构服务如rpc提供支持. 作为微服务系统的核 ...
- 后端渲染html、前端模板渲染html,jquery的html
作者:赵魏璇链接:https://www.zhihu.com/question/28725977/answer/116177149来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- 解决 eclipse tomcat cannot create a server using the selected type
解决的方法 1.退出eclipse: 2.打开 [工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings目录: 3.删除org.ecl ...
- Python VIL Realse
#!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...
- 峰Spring4学习(6)spring AOP的应用例子
一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...
- ROS使用国内的DDNS服务
未测试.转载余松老师的作品 虽然RouterOS 加入了cloud功能,但最近在配置RB2011的时候发现不好使,更新域名后无法正确解析到我的IP地址,虽然在cloud的public address中 ...
- javascript中不存在块级作用域,所以要小心使用在块级作用域中的函数声明所带来的作用域混乱.
在javascript中函数的作用域是一个非常重要的概念. javascript中是没有块级作用域,但是有函数作用域的概念. 我们在开发的过程中,经常会遇到这样的问题, 某个函数我暂时不需要,不想声明 ...
- 温故而知新-XML和WEB服务器
1 xml除了空元素外都是有开始标记和结束标记的 2 XML可以设置自己的标记
- android 学习 之 布局(上)
学习安卓布局前,先了解三个属性值: 1.fill_parent: 设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间 2.match_parent: And ...
- JS计算字符长度、字节数 -- 转
一个汉字在UTF-8编码中占用几个字节? 占用3个字节的范围 U+2E80 - U+2EF3 : 0xE2 0xBA 0x80 - 0xE2 0xBB 0xB3 共 115 个 U+2F00 - U+ ...