Browse Source

SpringBeanJobFactory supports autowiring through ApplicationContext

Issue: SPR-17323
pull/1986/merge
Juergen Hoeller 7 years ago
parent
commit
19f3347932
  1. 3
      spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java
  2. 23
      spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java

3
spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

@ -619,6 +619,9 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
this.jobFactory = new AdaptableJobFactory(); this.jobFactory = new AdaptableJobFactory();
} }
if (this.jobFactory != null) { if (this.jobFactory != null) {
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
}
if (this.jobFactory instanceof SchedulerContextAware) { if (this.jobFactory instanceof SchedulerContextAware) {
((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext()); ((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
} }

23
spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,9 @@ import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessorFactory; import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
@ -41,11 +44,15 @@ import org.springframework.lang.Nullable;
* @see SchedulerFactoryBean#setJobFactory * @see SchedulerFactoryBean#setJobFactory
* @see QuartzJobBean * @see QuartzJobBean
*/ */
public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware { public class SpringBeanJobFactory extends AdaptableJobFactory
implements ApplicationContextAware, SchedulerContextAware {
@Nullable @Nullable
private String[] ignoredUnknownProperties; private String[] ignoredUnknownProperties;
@Nullable
private ApplicationContext applicationContext;
@Nullable @Nullable
private SchedulerContext schedulerContext; private SchedulerContext schedulerContext;
@ -62,6 +69,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
this.ignoredUnknownProperties = ignoredUnknownProperties; this.ignoredUnknownProperties = ignoredUnknownProperties;
} }
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override @Override
public void setSchedulerContext(SchedulerContext schedulerContext) { public void setSchedulerContext(SchedulerContext schedulerContext) {
this.schedulerContext = schedulerContext; this.schedulerContext = schedulerContext;
@ -74,7 +86,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
*/ */
@Override @Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object job = super.createJobInstance(bundle); Object job = (this.applicationContext != null ?
this.applicationContext.getAutowireCapableBeanFactory().createBean(
bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) :
super.createJobInstance(bundle));
if (isEligibleForPropertyPopulation(job)) { if (isEligibleForPropertyPopulation(job)) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
@ -95,6 +111,7 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
bw.setPropertyValues(pvs, true); bw.setPropertyValues(pvs, true);
} }
} }
return job; return job;
} }

Loading…
Cancel
Save