Quartz是一个开源的定时调度框架,支持集群部署。咱们能够经过其Java API来使用它,或者经过Spring来配置与管理,也能够结合使用两种方式。本文重点分析Quartz2.2.3与Spring4.3.0.RELEASE集成时的初始化过程。java
与Spring集成时一般须要在Spring配置文件中加入SchedulerFactoryBean这个工厂Bean,例如:spring
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="overwriteExistingJobs" value="true"/> <property name="configLocation" value="classpath:quartz.properties"/> </bean>
再来看看SchedulerFactoryBean的类定义:框架
public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {
从中看到其实现了FactoryBean、BeanNameAware、ApplicationContextAware、InitializingBean、DisposableBean等经常使用接口,这些接口的具体意义本文不做赘述,不了解的能够专门研究下Spring的原理和源码实现。根据Spring的原理咱们知道,若是Bean自己实现了InitializingBean接口,那么在Spring加载解析BeanDefinition,并初始化Bean后会调用SchedulerFactoryBean的afterPropertiesSet方法,这里只会挑出其中的关键代码进行分析。spa
在afterPropertiesSet中首先会初始化SchedulerFactory,代码以下:code