您的当前位置:首页如何在job 执行类中获得spring 的applicationContext

如何在job 执行类中获得spring 的applicationContext

2023-11-05 来源:乌哈旅游
架构:spring+struts2+ibatis+quartz
现象:想在job执行类中注入业务bean,结果没有成功。那只能获得applicationConxtext,然后getBean.
解决:
	private static final String APPLICATION_CONTEXT_KEY = "applicationContext";

public final ApplicationContext getApplicationContext(
JobExecutionContext context) throws JobExecutionException {
ApplicationContext applicationContext = null;
try {
applicationContext = (ApplicationContext) context.getScheduler()
.getContext().get(APPLICATION_CONTEXT_KEY);
} catch (SchedulerException e) {
throw new JobExecutionException(e);
}
return applicationContext;
}


Spring 与Quartz之间的配置,重点是最后一句注入:
	<bean id="scheduler" name="springQuartz" lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">
SpringScheduler
</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.jobStore.class">
org.quartz.impl.jdbcjobstore.JobStoreTX
</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">
org.quartz.impl.jdbcjobstore.StdJDBCDelegate
</prop>
<prop key="org.quartz.jobStore.tablePrefix">
meta_task_
</prop>
</props>
</property>
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>

因篇幅问题不能全部显示,请点此查看更多更全内容