Skip to content

Commit 72d9177

Browse files
committed
Minor refactoring
- Extract/Rearrange methods - Inline variables - Improve Javadoc formatting
1 parent 6707705 commit 72d9177

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.batch.core.configuration.annotation;
1717

1818
import java.util.Collection;
19+
import java.util.Map;
1920

2021
import javax.sql.DataSource;
2122

@@ -39,8 +40,8 @@
3940
import org.springframework.util.Assert;
4041

4142
/**
42-
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch. Customization is
43-
* available by implementing the {@link BatchConfigurer} interface. {@link BatchConfigurer}.
43+
* Base {@code Configuration} class providing common structure for enabling and using Spring Batch.
44+
* Customization is available by implementing the {@link BatchConfigurer} interface.
4445
*
4546
* @author Dave Syer
4647
* @author Michael Minella
@@ -57,8 +58,6 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial
5758

5859
private BatchConfigurer configurer;
5960

60-
private JobRegistry jobRegistry = new MapJobRegistry();
61-
6261
private JobBuilderFactory jobBuilderFactory;
6362

6463
private StepBuilderFactory stepBuilderFactory;
@@ -120,7 +119,7 @@ public StepBuilderFactory stepBuilders() throws Exception {
120119
*/
121120
@Bean
122121
public JobRegistry jobRegistry() throws Exception {
123-
return this.jobRegistry;
122+
return new MapJobRegistry();
124123
}
125124

126125
/**
@@ -133,10 +132,11 @@ public JobRegistry jobRegistry() throws Exception {
133132

134133
@Override
135134
public void setImportMetadata(AnnotationMetadata importMetadata) {
136-
AnnotationAttributes enabled = AnnotationAttributes.fromMap(importMetadata.getAnnotationAttributes(
137-
EnableBatchProcessing.class.getName(), false));
138-
Assert.notNull(enabled,
139-
"@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName());
135+
Map<String, Object> annotationAttributes =
136+
importMetadata.getAnnotationAttributes(EnableBatchProcessing.class.getName(), false);
137+
AnnotationAttributes enabled = AnnotationAttributes.fromMap(annotationAttributes);
138+
String message = "@EnableBatchProcessing is not present on importing class " + importMetadata.getClassName();
139+
Assert.notNull(enabled, message);
140140
}
141141

142142
@Override
@@ -156,23 +156,11 @@ protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers)
156156
return this.configurer;
157157
}
158158
if (configurers == null || configurers.isEmpty()) {
159-
DataSource dataSource;
160-
try {
161-
dataSource = this.context.getBean(DataSource.class);
162-
} catch (NoUniqueBeanDefinitionException exception) {
163-
throw new IllegalStateException(
164-
"Multiple data sources are defined in the application context and no primary candidate was found. " +
165-
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
166-
exception);
167-
} catch (NoSuchBeanDefinitionException exception) {
168-
throw new IllegalStateException(
169-
"To use the default BatchConfigurer, the application context must contain at least one data source.",
170-
exception);
171-
}
159+
DataSource dataSource = getDataSource();
172160
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
173161
configurer.initialize();
174162
this.configurer = configurer;
175-
return configurer;
163+
return this.configurer;
176164
}
177165
if (configurers.size() > 1) {
178166
throw new IllegalStateException(
@@ -183,4 +171,21 @@ protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers)
183171
return this.configurer;
184172
}
185173

174+
private DataSource getDataSource() {
175+
DataSource dataSource;
176+
try {
177+
dataSource = this.context.getBean(DataSource.class);
178+
} catch (NoUniqueBeanDefinitionException exception) {
179+
throw new IllegalStateException(
180+
"Multiple data sources are defined in the application context and no primary candidate was found. " +
181+
"To use the default BatchConfigurer, one of the data sources should be annotated with '@Primary'.",
182+
exception);
183+
} catch (NoSuchBeanDefinitionException exception) {
184+
throw new IllegalStateException(
185+
"To use the default BatchConfigurer, the application context must contain at least one data source.",
186+
exception);
187+
}
188+
return dataSource;
189+
}
190+
186191
}

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ public class DefaultBatchConfigurer implements BatchConfigurer {
4242
private JobLauncher jobLauncher;
4343
private JobExplorer jobExplorer;
4444

45-
/**
46-
* Sets the dataSource.
47-
*
48-
* @param dataSource The data source to use. Must not be {@code null}.
49-
*/
50-
public void setDataSource(DataSource dataSource) {
51-
Assert.notNull(dataSource, "DataSource must not be null");
52-
this.dataSource = dataSource;
53-
}
54-
55-
/**
56-
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
57-
*/
58-
public DataSource getDataSource() {
59-
return this.dataSource;
60-
}
61-
6245
/**
6346
* Create a new {@link DefaultBatchConfigurer} with the passed datasource. This constructor
6447
* will configure a default {@link DataSourceTransactionManager}.
@@ -81,24 +64,42 @@ public DefaultBatchConfigurer(DataSource dataSource, PlatformTransactionManager
8164
this.transactionManager = transactionManager;
8265
}
8366

84-
@Override
85-
public JobRepository getJobRepository() {
86-
return jobRepository;
67+
/**
68+
* Sets the dataSource.
69+
*
70+
* @param dataSource The data source to use. Must not be {@code null}.
71+
*/
72+
public void setDataSource(DataSource dataSource) {
73+
Assert.notNull(dataSource, "DataSource must not be null");
74+
this.dataSource = dataSource;
8775
}
8876

77+
/**
78+
* @return The {@link DataSource} used by the {@link DefaultBatchConfigurer}.
79+
*/
80+
public DataSource getDataSource() {
81+
return this.dataSource;
82+
}
83+
84+
8985
@Override
90-
public PlatformTransactionManager getTransactionManager() {
91-
return transactionManager;
86+
public JobRepository getJobRepository() {
87+
return this.jobRepository;
9288
}
9389

9490
@Override
9591
public JobLauncher getJobLauncher() {
96-
return jobLauncher;
92+
return this.jobLauncher;
9793
}
9894

9995
@Override
10096
public JobExplorer getJobExplorer() {
101-
return jobExplorer;
97+
return this.jobExplorer;
98+
}
99+
100+
@Override
101+
public PlatformTransactionManager getTransactionManager() {
102+
return this.transactionManager;
102103
}
103104

104105
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Convenient factory for a {@link JobBuilder} which sets the {@link JobRepository} automatically.
2323
*
2424
* @author Dave Syer
25+
* @author Mahmoud Ben Hassine
2526
*
2627
*/
2728
public class JobBuilderFactory {
@@ -43,8 +44,7 @@ public JobBuilderFactory(JobRepository jobRepository) {
4344
* @return a job builder
4445
*/
4546
public JobBuilder get(String name) {
46-
JobBuilder builder = new JobBuilder(name).repository(jobRepository);
47-
return builder;
47+
return new JobBuilder(name).repository(this.jobRepository);
4848
}
4949

5050
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* {@link PlatformTransactionManager} automatically.
2525
*
2626
* @author Dave Syer
27+
* @author Mahmoud Ben Hassine
2728
*
2829
*/
2930
public class StepBuilderFactory {
@@ -51,9 +52,9 @@ public StepBuilderFactory(JobRepository jobRepository, PlatformTransactionManage
5152
* @return a step builder
5253
*/
5354
public StepBuilder get(String name) {
54-
StepBuilder builder = new StepBuilder(name).repository(jobRepository).transactionManager(
55-
transactionManager);
56-
return builder;
55+
return new StepBuilder(name)
56+
.repository(this.jobRepository)
57+
.transactionManager(this.transactionManager);
5758
}
5859

5960
}

0 commit comments

Comments
 (0)