1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如:
public class MyJobFactory extends org.springframework.scheduling.quartz.SpringBeanJobFactory
{
@Autowired
private AutowireCapableBeanFactory beanFactory;
/**
* 这里覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
beanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
2)定义上述类之后,需要在定义触发器,引用org.springframework.scheduling.quartz.SpringBeanJobFactory的地方,配置property,例如(见红色部分):
<bean name="quartzScheduler" lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="ds" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref bean="trigger" />
</list>
</property>
<property name="jobFactory">
<bean class="com.xxx.MyJobFactory" />
</property>
</bean>