解决Spring+Quartz没法自动注入bean问题

问题

咱们有时须要执行一些定时任务(如数据批处理),比较经常使用的技术框架有Spring + Quartz中。无奈此方式有个问题:Spring Bean没法自动注入。spring

环境:Spring3.2.2 + Quartz1.6.1框架

Quartz配置:this

 

<bean id="traderRiskReportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="traderNoServerResource" />
<property name="targetMethod" value="queryTraderNo" />
<property name="concurrent" value="true" />
</bean>

 

 

service配置:spa

 

<bean name="traderNoServerResource" class="com.test.TraderNoServerResource" >
  <property name="threadPool" ref="threadPool"/>
</bean>

 

 

ThreadPool配置:code

 <bean name="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
    <property name="corePoolSize" value="25"></property>
    <property name="maxPoolSize" value="100"></property>
 </bean>

 

 

出现的问题是:traderNoServerResource中的threadPool为null。blog

解决方法

    1. 成员变量添加注解@Autowiredget

    2. 而后在方法中(如例子中的queryTraderNo方法)添加如下代码,自动注入成员变量实现类io

      SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);class

       

       

 

关于引起这个问题的缘由,有待深刻验证。说的比较多的是Quartz与SpringMVC的context不一样,父context没法访问子context中的bean。thread

参考资料

http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring

相关文章
相关标签/搜索