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

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -22,6 +22,9 @@ import org.quartz.spi.TriggerFiredBundle; @@ -22,6 +22,9 @@ import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MutablePropertyValues;
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;
/**
@ -41,11 +44,15 @@ import org.springframework.lang.Nullable; @@ -41,11 +44,15 @@ import org.springframework.lang.Nullable;
* @see SchedulerFactoryBean#setJobFactory
* @see QuartzJobBean
*/
public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware {
public class SpringBeanJobFactory extends AdaptableJobFactory
implements ApplicationContextAware, SchedulerContextAware {
@Nullable
private String[] ignoredUnknownProperties;
@Nullable
private ApplicationContext applicationContext;
@Nullable
private SchedulerContext schedulerContext;
@ -62,6 +69,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul @@ -62,6 +69,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
this.ignoredUnknownProperties = ignoredUnknownProperties;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void setSchedulerContext(SchedulerContext schedulerContext) {
this.schedulerContext = schedulerContext;
@ -74,7 +86,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul @@ -74,7 +86,11 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
*/
@Override
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)) {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
MutablePropertyValues pvs = new MutablePropertyValues();
@ -95,6 +111,7 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul @@ -95,6 +111,7 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
bw.setPropertyValues(pvs, true);
}
}
return job;
}

Loading…
Cancel
Save