弹簧自动装配不适用于石英

时间:2014-06-02 09:43:09

标签: java spring quartz-scheduler

我有一个Web应用程序我使用spring应用程序上下文来注入我的bean,在我的应用程序中注释了@Autowired我有一个服务有一个生成报告的方法,这个服务是这样的:

public ReportingService extends CommonService {
//this method is called from a controller to generate a report right now
//after filling the parameters of the report i.e. businessday
public generateReport(Request request) {.....}  
public generateScheduledReport() {
//read configured parameters from database and fill request
Request rr = ...;
generateReport(request);
}

对于计划的报告我已经定义了一个扩展QuartzJobBea`的作业,并使用我的ReportingService的一个字段来调用它的generateScheduledReport,如下所示:

public class ScheduledReportJob extends QuartzJobBean {
@Autowired
@Qualifier("reportScheduler")
CommonService reportScheduler;
//getters and setters  
@Override
protected void executeInternal(JobExecutionContext arg0)
        throws JobExecutionException {
    reportScheduler.generateScheduledReport();

在我的applicationContext中,我按如下方式设置了quartz和spring bean:

 <bean id"reportScheduler" class="com.monim.ReportingService"/>
 <bean name="schedulingjob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
     <property name="jobClass"
        value="com.monim.ScheduledReportJob " />
     <property name="name" value="name" />
     <property name="group" value="group"/>
     <property name="durability" value="true" />
 </bean>
 <bean id="trigger"
    class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
     <property name="name" value="name" />
     <property name="group" value="group" />
     <property name="jobDetail" ref="schedulingjob" />  
     <property name="cronExpression" value="0/5 * * * * ?" />
    <!-- MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY - SEE MISFIRE INSTRUCTIONS 
    <property name="misfireInstruction" value="1" />  
  <bean id="Scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    p:waitForJobsToCompleteOnShutdown="false" lazy-init="true">
     <property name="dataSource" ref="dataSource" />
     <property name="transactionManager" ref="transactionManager" />  
    <property name="overwriteExistingJobs" value="false" />
    <property name="quartzProperties">  
 <map>  
 <!-- scheduler configuration -->
 <entry key="org.quartz.scheduler.instanceName" value="scheduler" />
 <entry key="org.quartz.scheduler.skipUpdateCheck" value="true" />
 <entry key="org.quartz.jobStore.misfireThreshold" value="60000" />
 <!-- JobStore configuration -->
 <entry key="org.quartz.jobStore.class" value="org.quartz.impl.jdbcjobstore.JobStoreCMT" />
 <entry key="org.quartz.jobStore.dataSource" value="dataSource" />
 <entry key="org.quartz.jobStore.nonManagedTXDataSource" value="quartzDataSource" />
 <entry key="org.quartz.jobStore.driverDelegateClass" value="org.quartz.impl.jdbcjobstore.oracle.OracleDelegate"/>
<entry key="org.quartz.jobStore.tablePrefix" value="QRTZ_" />
<entry key="org.quartz.jobStore.useProperties" value="true" />
<entry key="org.quartz.jobStore.selectWithLockSQL"
value="SELECT * FROM {0}LOCKS WHERE SCHED_NAME = {1} AND LOCK_NAME = ? FOR UPDATE" />
<entry key="org.quartz.jobStore.isClustered" value="false" />
<!-- ThreadPool configuration -->
<entry key="org.quartz.threadPool.class" value="org.quartz.simpl.SimpleThreadPool" />
<entry key="org.quartz.threadPool.threadCount" value="3" />
</map>  
</property>  
<property name="applicationContextSchedulerContextKey">
    <value>applicationContext</value>
</property>
<property name="jobDetails">
<list>
<ref bean="schedulingjob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="trigger" />
</list>
</property>
</bean>

我的ReportingService包含对其他bean的引用,这些bean也是通过应用程序上下文注入的。我的问题是调度程序正在运行并已成功启动但是用于调度作业的所有注入bean都被初始化为null,请帮助

1 个答案:

答案 0 :(得分:5)

搜索了许多主题和博客后,我通过执行以下操作让我的应用程序正常工作: 方法的第一行executeInternal我把这一行:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 我从答案here中找到了 然后我启动了应用程序&#34;仍未注入豆类&#34;但这使得石英将其触发器保存到数据库中,以后将使用。之后我将这些属性添加到我的作业bean中:

  <property name="jobDataAsMap">
    <map>
      <entry key="schedulerTask" value-ref="reportWithTWService" />
     </map>
  </property>

一切都很顺利。但是当用石英表添加这些属性时,它不起作用,我希望如果有人能向我解释发生了什么