15
15
*/
16
16
package org .springframework .batch .core .configuration .annotation ;
17
17
18
+ import java .util .Collection ;
18
19
import java .util .concurrent .atomic .AtomicReference ;
19
20
20
21
import org .aopalliance .intercept .MethodInterceptor ;
34
35
import org .springframework .transaction .PlatformTransactionManager ;
35
36
36
37
/**
37
- * Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
38
- * available by implementing the {@link BatchConfigurer} interface. The main components are created as lazy proxies that
39
- * only initialize when a method is called. This is to prevent (as much as possible) configuration cycles from
40
- * developing when these components are needed in a configuration resource that itself provides a
41
- * {@link BatchConfigurer}.
38
+ * Base {@code Configuration} class providing common structure for enabling and using Spring Batch.
39
+ * Customization is available by implementing the {@link BatchConfigurer} interface.
42
40
*
43
41
* @author Dave Syer
44
42
* @author Mahmoud Ben Hassine
@@ -51,98 +49,36 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
51
49
@ Autowired
52
50
private ApplicationContext context ;
53
51
54
- private boolean initialized = false ;
55
-
56
- private AtomicReference <JobRepository > jobRepository = new AtomicReference <>();
57
-
58
- private AtomicReference <JobLauncher > jobLauncher = new AtomicReference <>();
59
-
60
- private AtomicReference <JobRegistry > jobRegistry = new AtomicReference <>();
61
-
62
- private AtomicReference <PlatformTransactionManager > transactionManager = new AtomicReference <>();
63
-
64
- private AtomicReference <JobExplorer > jobExplorer = new AtomicReference <>();
52
+ @ Autowired (required = false )
53
+ private Collection <BatchConfigurer > configurers ;
65
54
66
55
@ Override
67
56
@ Bean
68
57
public JobRepository jobRepository () throws Exception {
69
- return createLazyProxy ( jobRepository , JobRepository . class );
58
+ return getConfigurer ( configurers ). getJobRepository ( );
70
59
}
71
60
72
61
@ Override
73
62
@ Bean
74
63
public JobLauncher jobLauncher () throws Exception {
75
- return createLazyProxy ( jobLauncher , JobLauncher . class );
64
+ return getConfigurer ( configurers ). getJobLauncher ( );
76
65
}
77
66
78
67
@ Override
79
68
@ Bean
80
69
public JobRegistry jobRegistry () throws Exception {
81
- return createLazyProxy ( jobRegistry , JobRegistry . class );
70
+ return new MapJobRegistry ( );
82
71
}
83
72
84
73
@ Override
85
74
@ Bean
86
- public JobExplorer jobExplorer () {
87
- return createLazyProxy ( jobExplorer , JobExplorer . class );
75
+ public JobExplorer jobExplorer () throws Exception {
76
+ return getConfigurer ( configurers ). getJobExplorer ( );
88
77
}
89
78
90
79
@ Override
91
80
public PlatformTransactionManager transactionManager () throws Exception {
92
- return createLazyProxy (transactionManager , PlatformTransactionManager .class );
93
- }
94
-
95
- private <T > T createLazyProxy (AtomicReference <T > reference , Class <T > type ) {
96
- ProxyFactory factory = new ProxyFactory ();
97
- factory .setTargetSource (new ReferenceTargetSource <>(reference ));
98
- factory .addAdvice (new PassthruAdvice ());
99
- factory .setInterfaces (new Class <?>[] { type });
100
- @ SuppressWarnings ("unchecked" )
101
- T proxy = (T ) factory .getProxy ();
102
- return proxy ;
103
- }
104
-
105
- /**
106
- * Sets up the basic components by extracting them from the {@link BatchConfigurer configurer}, defaulting to some
107
- * sensible values as long as a unique DataSource is available.
108
- *
109
- * @throws Exception if there is a problem in the configurer
110
- */
111
- protected void initialize () throws Exception {
112
- if (initialized ) {
113
- return ;
114
- }
115
- BatchConfigurer configurer = getConfigurer (context .getBeansOfType (BatchConfigurer .class ).values ());
116
- jobRepository .set (configurer .getJobRepository ());
117
- jobLauncher .set (configurer .getJobLauncher ());
118
- transactionManager .set (configurer .getTransactionManager ());
119
- jobRegistry .set (new MapJobRegistry ());
120
- jobExplorer .set (configurer .getJobExplorer ());
121
- initialized = true ;
122
- }
123
-
124
- private class PassthruAdvice implements MethodInterceptor {
125
-
126
- @ Override
127
- public Object invoke (MethodInvocation invocation ) throws Throwable {
128
- return invocation .proceed ();
129
- }
130
-
131
- }
132
-
133
- private class ReferenceTargetSource <T > extends AbstractLazyCreationTargetSource {
134
-
135
- private AtomicReference <T > reference ;
136
-
137
- public ReferenceTargetSource (AtomicReference <T > reference ) {
138
- this .reference = reference ;
139
- }
140
-
141
- @ Override
142
- protected Object createObject () throws Exception {
143
- initialize ();
144
- return reference .get ();
145
- }
81
+ return getConfigurer (configurers ).getTransactionManager ();
146
82
}
147
83
148
84
}
0 commit comments