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.我们的目的是什么? ...
随机推荐
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案
错误代码如下: 严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException ...
- js控制div是否显示
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- css新增UI样式
1.圆角 border-radius <style> .box{width:200px;height:300px;border:1px solid #000;border-radius:1 ...
- codeforces 432 B Football Kit
题意:给出n支球队,主场的衣服的颜色x[i],客场的衣服的颜色y[i],每只队伍会打n-1次主场,n-1次客场,当这只队伍打客场的衣服颜色和对手一样的时候,那么他们要穿上主场的衣服 问每个球队主场球服 ...
- Object-C 内存管理及对象
关于OC 的内存管理是使用 引用计数的方式 进行管理的引用计数可以使用 办公室的开关灯 来说明 如下图与 OC对象 对应如下
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
- C++编程常见错误
1.成员变量要记得在构造函数中初始化 2.还是初始化!初始化!初始化!
- IOS 单例 创建方式
@implementation Me static Car *sharedInstance= nil;//声明一个静态对象引用并赋为nil +(Me *) sharedInstance//声明类方法( ...
- Solution multisite htaccess cleanURL
My solution to getting Clean URL working with my multisite setup drupal 4.7 I added Alias to my http ...
- Groovy获取json和xml数据
如果是xml就用这个 // to read a node from your Response def grUtils = new com.eviware.soapui.support.GroovyU ...