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.
2222import org .springframework .beans .BeanWrapper ;
2323import org .springframework .beans .MutablePropertyValues ;
2424import org .springframework .beans .PropertyAccessorFactory ;
25+ import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
26+ import org .springframework .context .ApplicationContext ;
27+ import org .springframework .context .ApplicationContextAware ;
2528import org .springframework .lang .Nullable ;
2629
2730/**
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