quartz ha 一点疑惑
public class MyClusterDetailQuartzJobBean extends QuartzJobBean implements StatefulJob{
protected final Logger logger = LoggerFactory
.getLogger(ClusterDetailQuartzJobBean.class);
// 上下文
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
// 对象
private String targetObject;
public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
}
// 方法
private String targetMethod;
public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
logger.debug("execute [" + targetobject + "] at once >>>>>>");
try {
// 从Spring获取对象
Object otargetObject = applicationContext.getBean(targetObject);
// 获取方法
Method m = otargetObject.getClass().getMethod(targetMethod,
new Class[] {JobExecutionContext.class});
// 执行
m.invoke(otargetObject, new Object[] { context });
} catch (Exception e) {
logger.error("", e);
throw new JobExecutionException(e);
}
}
这里的applicationContext 如何注入进来的?
org.springframework.scheduling.quartz.JobDetailBean 里面一段注释
/** * Set the key of an ApplicationContext reference to expose in the JobDataMap, * for example "applicationContext". Default is none. * Only applicable when running in a Spring ApplicationContext. * In case of a QuartzJobBean, the reference will be applied to the Job * instance as bean property. An "applicationContext" attribute will correspond * to a "setApplicationContext" method in that scenario. * Note that BeanFactory callback interfaces like ApplicationContextAware * are not automatically applied to Quartz Job instances, because Quartz * itself is responsible for the lifecycle of its Jobs. * Note: When using persistent job stores where JobDetail contents will * be kept in the database, do not put an ApplicationContext reference into * the JobDataMap but rather into the SchedulerContext. * @see SchedulerFactoryBean#setApplicationContextSchedulerContextKey * @see org.springframework.context.ApplicationContext */