Skip to content

Commit 31af573

Browse files
committed
Remove the lazy proxy creation of batch artifacts in batch configuration
The lazy proxy creation of batch artifacts was intended to prevent configuration cycles from happening. However, this is implemented in SimpleBatchConfiguration but not in ModularBatchConfiguration. Removing this laziness in the configuration process does not introduce any regression neither in tests nor in samples. This commit makes SimpleBatchConfiguration consistent with ModularBatchConfiguration.
1 parent 234e28a commit 31af573

File tree

1 file changed

+11
-75
lines changed

1 file changed

+11
-75
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java

Lines changed: 11 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.configuration.annotation;
1717

18+
import java.util.Collection;
1819
import java.util.concurrent.atomic.AtomicReference;
1920

2021
import org.aopalliance.intercept.MethodInterceptor;
@@ -34,11 +35,8 @@
3435
import org.springframework.transaction.PlatformTransactionManager;
3536

3637
/**
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.
4240
*
4341
* @author Dave Syer
4442
* @author Mahmoud Ben Hassine
@@ -51,98 +49,36 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
5149
@Autowired
5250
private ApplicationContext context;
5351

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;
6554

6655
@Override
6756
@Bean
6857
public JobRepository jobRepository() throws Exception {
69-
return createLazyProxy(jobRepository, JobRepository.class);
58+
return getConfigurer(configurers).getJobRepository();
7059
}
7160

7261
@Override
7362
@Bean
7463
public JobLauncher jobLauncher() throws Exception {
75-
return createLazyProxy(jobLauncher, JobLauncher.class);
64+
return getConfigurer(configurers).getJobLauncher();
7665
}
7766

7867
@Override
7968
@Bean
8069
public JobRegistry jobRegistry() throws Exception {
81-
return createLazyProxy(jobRegistry, JobRegistry.class);
70+
return new MapJobRegistry();
8271
}
8372

8473
@Override
8574
@Bean
86-
public JobExplorer jobExplorer() {
87-
return createLazyProxy(jobExplorer, JobExplorer.class);
75+
public JobExplorer jobExplorer() throws Exception {
76+
return getConfigurer(configurers).getJobExplorer();
8877
}
8978

9079
@Override
9180
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();
14682
}
14783

14884
}

0 commit comments

Comments
 (0)