1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import org .springframework .beans .BeanWrapper ;
23
23
import org .springframework .beans .MutablePropertyValues ;
24
24
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 ;
25
28
import org .springframework .lang .Nullable ;
26
29
27
30
/**
41
44
* @see SchedulerFactoryBean#setJobFactory
42
45
* @see QuartzJobBean
43
46
*/
44
- public class SpringBeanJobFactory extends AdaptableJobFactory implements SchedulerContextAware {
47
+ public class SpringBeanJobFactory extends AdaptableJobFactory
48
+ implements ApplicationContextAware , SchedulerContextAware {
45
49
46
50
@ Nullable
47
51
private String [] ignoredUnknownProperties ;
48
52
53
+ @ Nullable
54
+ private ApplicationContext applicationContext ;
55
+
49
56
@ Nullable
50
57
private SchedulerContext schedulerContext ;
51
58
@@ -62,6 +69,11 @@ public void setIgnoredUnknownProperties(String... ignoredUnknownProperties) {
62
69
this .ignoredUnknownProperties = ignoredUnknownProperties ;
63
70
}
64
71
72
+ @ Override
73
+ public void setApplicationContext (ApplicationContext applicationContext ) {
74
+ this .applicationContext = applicationContext ;
75
+ }
76
+
65
77
@ Override
66
78
public void setSchedulerContext (SchedulerContext schedulerContext ) {
67
79
this .schedulerContext = schedulerContext ;
@@ -74,7 +86,11 @@ public void setSchedulerContext(SchedulerContext schedulerContext) {
74
86
*/
75
87
@ Override
76
88
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
+
78
94
if (isEligibleForPropertyPopulation (job )) {
79
95
BeanWrapper bw = PropertyAccessorFactory .forBeanPropertyAccess (job );
80
96
MutablePropertyValues pvs = new MutablePropertyValues ();
@@ -95,6 +111,7 @@ protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
95
111
bw .setPropertyValues (pvs , true );
96
112
}
97
113
}
114
+
98
115
return job ;
99
116
}
100
117
0 commit comments