Skip to content

Commit 19f3347

Browse files
committed
SpringBeanJobFactory supports autowiring through ApplicationContext
Issue: SPR-17323
1 parent efdbddd commit 19f3347

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,9 @@ private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws Sch
619619
this.jobFactory = new AdaptableJobFactory();
620620
}
621621
if (this.jobFactory != null) {
622+
if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
623+
((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
624+
}
622625
if (this.jobFactory instanceof SchedulerContextAware) {
623626
((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
624627
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,9 @@
2222
import org.springframework.beans.BeanWrapper;
2323
import org.springframework.beans.MutablePropertyValues;
2424
import org.springframework.beans.PropertyAccessorFactory;
25+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.context.ApplicationContextAware;
2528
import org.springframework.lang.Nullable;
2629

2730
/**
@@ -41,11 +44,15 @@
4144
* @see SchedulerFactoryBean#setJobFactory
4245
* @see QuartzJobBean
4346
*/
44-
public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware {
47+
public class SpringBeanJobFactory extends AdaptableJobFactory
48+
implements ApplicationContextAware, SchedulerContextAware {
4549

4650
@Nullable
4751
private String[] ignoredUnknownProperties;
4852

53+
@Nullable
54+
private ApplicationContext applicationContext;
55+
4956
@Nullable
5057
private SchedulerContext schedulerContext;
5158

@@ -62,6 +69,11 @@ public void setIgnoredUnknownProperties(String... ignoredUnknownProperties) {
6269
this.ignoredUnknownProperties = ignoredUnknownProperties;
6370
}
6471

72+
@Override
73+
public void setApplicationContext(ApplicationContext applicationContext) {
74+
this.applicationContext = applicationContext;
75+
}
76+
6577
@Override
6678
public void setSchedulerContext(SchedulerContext schedulerContext) {
6779
this.schedulerContext = schedulerContext;
@@ -74,7 +86,11 @@ public void setSchedulerContext(SchedulerContext schedulerContext) {
7486
*/
7587
@Override
7688
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
77-
Object job = super.createJobInstance(bundle);
89+
Object job = (this.applicationContext != null ?
90+
this.applicationContext.getAutowireCapableBeanFactory().createBean(
91+
bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) :
92+
super.createJobInstance(bundle));
93+
7894
if (isEligibleForPropertyPopulation(job)) {
7995
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
8096
MutablePropertyValues pvs = new MutablePropertyValues();
@@ -95,6 +111,7 @@ protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
95111
bw.setPropertyValues(pvs, true);
96112
}
97113
}
114+
98115
return job;
99116
}
100117

0 commit comments

Comments
 (0)