quartz 的job中获取到applicationContext
第一步:
定义SchedulerFactoryBean的applicationContextSchedulerContextKey
<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!-- 注入数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 延迟30秒启动Scheduler -->
<property name="startupDelay" value="30"></property>
<!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 -->
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
</bean>
定义之后,产生的效果是:
this.scheduler.getContext().put(this.applicationContextSchedulerContextKey, this.applicationContext);
第二步:
获取到scheduler,然后从中取出applicationContext 即可
public class TestJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
Scheduler scheduler = (Scheduler) context.getScheduler();
//获取JobExecutionContext中的service对象
try {
//获取JobExecutionContext中的service对象
SchedulerContext schCtx = context.getScheduler().getContext();
//获取Spring中的上下文
ApplicationContext appCtx = (ApplicationContext)schCtx.get("applicationContext");
jobService= (JobService)appCtx.getBean("jobService");
....
} catch (SchedulerException e1) {
// TODO 尚未处理异常
e1.printStackTrace();
}
}
};
quartz 的job中获取到applicationContext的更多相关文章
- 如何在Job中获取 IOC applicationcontext
如何在Job中获取 IOC applicationcontext https://segmentfault.com/q/1010000008002800 SpringBoot之整合Quartz调度框架 ...
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- SpringBoot项目中获取applicationContext对象
ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系 ...
- Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- quartz的job怎么获取Spring上下文
第一步.在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerConte ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
- tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)
//从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = Conte ...
- 从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...
随机推荐
- 部署新浪SAE web.py Session及图片上传等问题注意事项
1.以下几条代码解决编码问题 import sysreload(sys)sys.setdefaultencoding('utf-8') 2.图片上传问题 需要开通sina的Storage服务,随便建个 ...
- STL set容器的一点总结
整理了一下set常用语句,参看这篇http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html -------------- ...
- Using unique option prefix myisam-recover instead of myisam-recover-option
[转载]关于mysql error.log报"Using unique option prefix myisam-recover instead of myisam-recover-opti ...
- [Swift 语法点滴]—— Struct Vs Class
摘自:stackoverflow.com/questions/24232799/why-choose-struct-over-class Structure instances are always ...
- Google Code Pretiffy 代码 着色 高亮 开源 javascript(JS)库
1.简介 introduction Google Code Pretiffy 是 Google 的一个用来对代码进行语法着色的 JavaScript 库,支持 C/C++, Java, Python, ...
- dede 替换后台两个文件去广告
A:替换后台两个文件去广告 dede/templets路径下两个文件 1.index2.htm <!--This is IE DTD patch , Don't delete this lin ...
- Oracle时间函数numtoyminterval()
格式:NumToYMInterval(n, interval_unit); n: 数值类型 interval_unit: 'YEAR', 'MONTH' ,或其他可以转换成这两个值之一的表达式 N ...
- 删除binlog的方法
不知道你有没有为mysql的binlog占用大量磁盘感到无奈,my.cnf里binlog的size可以设置多大做分割,但没有看到删除的配置,在mysql里show了一下variables, mysql ...
- nginx上传目录配置,禁止执行权限
我们经常会把网站的图片文件上传目录设置为只可上传文件但不能执行文件,就是要禁止执行权限,小编来给大家举一个上传目录配置,禁止执行权限方法,各位可参考. 如果不让有执行权限最简单的办法 代码如下 复制 ...
- 使用SchemaSpy逆向工程生成数据库依赖关系使用SchemaSpy工具可以快速的从数据库中得到
使用SchemaSpy逆向工程生成数据库依赖关系 使用SchemaSpy工具可以快速的从数据库中得到表的依赖关系,同时生成一个生动的“表图”结合的报告.方便快速了解数据库中的数据库对象间关系,类 ...