diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java index 3124dc726aa..9a8ae301324 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java +++ b/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(); } 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()); } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java index efcc289b203..93e495f4aae 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java +++ b/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"); * 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.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; * @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 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 */ @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 bw.setPropertyValues(pvs, true); } } + return job; }