Skip to content

Commit 6e443cb

Browse files
committed
Deprecate Job/Step builder factories
This commit deprecates JobBuilderFactory and StepBuilderFactory in favor of the respective builders they create. It also removes the exposure of such utilities as beans in the application context when using `@EnableBatchProcessing`. Resolves #4188
1 parent 06c2dc3 commit 6e443cb

File tree

45 files changed

+387
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+387
-484
lines changed

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

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,15 @@
4949
*/
5050
@Configuration(proxyBeanMethods = false)
5151
@Import(ScopeConfiguration.class)
52-
public abstract class AbstractBatchConfiguration implements InitializingBean {
52+
public abstract class AbstractBatchConfiguration {
5353

5454
private static final Log logger = LogFactory.getLog(AbstractBatchConfiguration.class);
5555

5656
@Autowired
5757
protected ApplicationContext context;
5858

59-
private JobBuilderFactory jobBuilderFactory;
60-
61-
private StepBuilderFactory stepBuilderFactory;
62-
6359
private JobRegistry jobRegistry = new MapJobRegistry();
6460

65-
/**
66-
* Establish the {@link JobBuilderFactory} for the batch execution.
67-
* @return The instance of the {@link JobBuilderFactory}.
68-
* @throws Exception The {@link Exception} thrown if an error occurs.
69-
*/
70-
@Bean
71-
public JobBuilderFactory jobBuilders() throws Exception {
72-
return this.jobBuilderFactory;
73-
}
74-
75-
/**
76-
* Establish the {@link StepBuilderFactory} for the batch execution.
77-
* @return The instance of the {@link StepBuilderFactory}.
78-
* @throws Exception The {@link Exception} thrown if an error occurs.
79-
*/
80-
@Bean
81-
public StepBuilderFactory stepBuilders() throws Exception {
82-
return this.stepBuilderFactory;
83-
}
84-
8561
/**
8662
* Establish the {@link JobRepository} for the batch execution.
8763
* @return The instance of the {@link JobRepository}.
@@ -123,13 +99,6 @@ public JobRegistry jobRegistry() throws Exception {
12399
*/
124100
public abstract PlatformTransactionManager transactionManager() throws Exception;
125101

126-
@Override
127-
public void afterPropertiesSet() throws Exception {
128-
BatchConfigurer batchConfigurer = getOrCreateConfigurer();
129-
this.jobBuilderFactory = new JobBuilderFactory(batchConfigurer.getJobRepository());
130-
this.stepBuilderFactory = new StepBuilderFactory(batchConfigurer.getJobRepository());
131-
}
132-
133102
/**
134103
* If a {@link BatchConfigurer} exists, return it. Otherwise, create a
135104
* {@link DefaultBatchConfigurer}. If more than one configurer is present, an

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
* public class AppConfig {
4545
*
4646
* @Autowired
47-
* private JobBuilderFactory jobs;
47+
* private JobRepository jobRepository;
4848
*
4949
* @Bean
50-
* public Job job() {
51-
* return jobs.get("myJob").start(step1()).next(step2()).build();
50+
* public Job job(JobRepository jobRepository) {
51+
* return new JobBuilder("myJob").repository(jobRepository).start(step1()).next(step2()).build();
5252
* }
5353
*
5454
* @Bean
@@ -108,12 +108,6 @@
108108
* <li>a {@link org.springframework.batch.core.explore.JobExplorer} (bean name
109109
* "jobExplorer" of type
110110
* {@link org.springframework.batch.core.explore.support.SimpleJobExplorer})</li>
111-
* <li>a {@link JobBuilderFactory} (bean name "jobBuilders") as a convenience to prevent
112-
* you from having to inject the job repository into every job, as in the earlier
113-
* examples</li>
114-
* <li>a {@link StepBuilderFactory} (bean name "stepBuilders") as a convenience to prevent
115-
* you from having to inject the job repository and transaction manager into every
116-
* step</li>
117111
* </ul>
118112
*
119113
* The transaction manager provided by this annotation is of type

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
*
2626
* @author Dave Syer
2727
* @author Mahmoud Ben Hassine
28+
* @deprecated Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using
29+
* the {@link JobBuilder}.
2830
*
2931
*/
32+
@Deprecated(since = "5.0.0", forRemoval = true)
3033
public class JobBuilderFactory {
3134

3235
private JobRepository jobRepository;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
*
2727
* @author Dave Syer
2828
* @author Mahmoud Ben Hassine
29+
* @deprecated Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using
30+
* the {@link StepBuilder}.
2931
*
3032
*/
33+
@Deprecated(since = "5.0.0", forRemoval = true)
3134
public class StepBuilderFactory {
3235

3336
private JobRepository jobRepository;

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/InlineDataSourceDefinitionTests.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.springframework.batch.core.Job;
2525
import org.springframework.batch.core.JobExecution;
2626
import org.springframework.batch.core.JobParameters;
27+
import org.springframework.batch.core.job.builder.JobBuilder;
2728
import org.springframework.batch.core.launch.JobLauncher;
29+
import org.springframework.batch.core.repository.JobRepository;
30+
import org.springframework.batch.core.step.builder.StepBuilder;
2831
import org.springframework.batch.repeat.RepeatStatus;
2932
import org.springframework.context.ApplicationContext;
3033
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -53,21 +56,13 @@ void testInlineDataSourceDefinition() throws Exception {
5356
@EnableBatchProcessing
5457
static class MyJobConfiguration {
5558

56-
private JobBuilderFactory jobs;
57-
58-
private StepBuilderFactory steps;
59-
60-
public MyJobConfiguration(JobBuilderFactory jobs, StepBuilderFactory steps) {
61-
this.jobs = jobs;
62-
this.steps = steps;
63-
}
64-
6559
@Bean
66-
public Job job() {
67-
return jobs.get("job").start(steps.get("step").tasklet((contribution, chunkContext) -> {
68-
System.out.println("hello world");
69-
return RepeatStatus.FINISHED;
70-
}).build()).build();
60+
public Job job(JobRepository jobRepository) {
61+
return new JobBuilder("job").repository(jobRepository)
62+
.start(new StepBuilder("step").repository(jobRepository).tasklet((contribution, chunkContext) -> {
63+
System.out.println("hello world");
64+
return RepeatStatus.FINISHED;
65+
}).build()).build();
7166
}
7267

7368
@Bean

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobBuilderConfigurationTests.java

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import org.springframework.batch.core.Step;
3030
import org.springframework.batch.core.StepContribution;
3131
import org.springframework.batch.core.StepExecution;
32+
import org.springframework.batch.core.job.builder.JobBuilder;
3233
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
3334
import org.springframework.batch.core.launch.JobLauncher;
35+
import org.springframework.batch.core.repository.JobRepository;
3436
import org.springframework.batch.core.scope.context.ChunkContext;
3537
import org.springframework.batch.core.step.AbstractStep;
38+
import org.springframework.batch.core.step.builder.StepBuilder;
3639
import org.springframework.batch.core.step.tasklet.Tasklet;
3740
import org.springframework.batch.repeat.RepeatStatus;
3841
import org.springframework.beans.factory.annotation.Autowired;
@@ -110,28 +113,28 @@ private void testJob(String jobName, BatchStatus status, int stepExecutionCount,
110113
public static class TestConfiguration {
111114

112115
@Autowired
113-
private JobBuilderFactory jobs;
114-
115-
@Autowired
116-
private StepBuilderFactory steps;
116+
private JobRepository jobRepository;
117117

118118
@Autowired
119119
private JdbcTransactionManager transactionManager;
120120

121121
@Bean
122122
public Job testJob() throws Exception {
123-
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
123+
SimpleJobBuilder builder = new JobBuilder("test").repository(this.jobRepository).start(step1())
124+
.next(step2());
124125
return builder.build();
125126
}
126127

127128
@Bean
128129
protected Step step1() throws Exception {
129-
return steps.get("step1").tasklet(tasklet()).transactionManager(this.transactionManager).build();
130+
return new StepBuilder("step1").repository(jobRepository).tasklet(tasklet())
131+
.transactionManager(this.transactionManager).build();
130132
}
131133

132134
@Bean
133135
protected Step step2() throws Exception {
134-
return steps.get("step2").tasklet(tasklet()).transactionManager(this.transactionManager).build();
136+
return new StepBuilder("step2").repository(jobRepository).tasklet(tasklet())
137+
.transactionManager(this.transactionManager).build();
135138
}
136139

137140
@Bean
@@ -155,27 +158,22 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext context)
155158
@Import(DataSourceConfiguration.class)
156159
public static class AnotherConfiguration {
157160

158-
@Autowired
159-
private JobBuilderFactory jobs;
160-
161-
@Autowired
162-
private StepBuilderFactory steps;
163-
164161
@Autowired
165162
private JdbcTransactionManager transactionManager;
166163

167164
@Autowired
168165
private Tasklet tasklet;
169166

170167
@Bean
171-
public Job anotherJob() throws Exception {
172-
SimpleJobBuilder builder = jobs.get("another").start(step3());
168+
public Job anotherJob(JobRepository jobRepository) throws Exception {
169+
SimpleJobBuilder builder = new JobBuilder("another").repository(jobRepository).start(step3(jobRepository));
173170
return builder.build();
174171
}
175172

176173
@Bean
177-
protected Step step3() throws Exception {
178-
return steps.get("step3").tasklet(tasklet).transactionManager(this.transactionManager).build();
174+
protected Step step3(JobRepository jobRepository) throws Exception {
175+
return new StepBuilder("step3").repository(jobRepository).tasklet(tasklet)
176+
.transactionManager(this.transactionManager).build();
179177
}
180178

181179
}
@@ -185,16 +183,13 @@ protected Step step3() throws Exception {
185183
@Import(DataSourceConfiguration.class)
186184
public static class TestConfigurer extends DefaultBatchConfigurer {
187185

188-
@Autowired
189-
private SimpleBatchConfiguration jobs;
190-
191186
public TestConfigurer(DataSource dataSource) {
192187
super(dataSource);
193188
}
194189

195190
@Bean
196-
public Job testConfigurerJob() throws Exception {
197-
SimpleJobBuilder builder = jobs.jobBuilders().get("configurer").start(step1());
191+
public Job testConfigurerJob(JobRepository jobRepository) throws Exception {
192+
SimpleJobBuilder builder = new JobBuilder("configurer").repository(jobRepository).start(step1());
198193
return builder.build();
199194
}
200195

@@ -218,24 +213,18 @@ protected void doExecute(StepExecution stepExecution) throws Exception {
218213
@Import(DataSourceConfiguration.class)
219214
public static class BeansConfigurer {
220215

221-
@Autowired
222-
private JobBuilderFactory jobs;
223-
224-
@Autowired
225-
private StepBuilderFactory steps;
226-
227216
@Autowired
228217
private JdbcTransactionManager transactionManager;
229218

230219
@Bean
231-
public Job beansConfigurerJob() throws Exception {
232-
SimpleJobBuilder builder = jobs.get("beans").start(step1());
220+
public Job beansConfigurerJob(JobRepository jobRepository) throws Exception {
221+
SimpleJobBuilder builder = new JobBuilder("beans").repository(jobRepository).start(step1(jobRepository));
233222
return builder.build();
234223
}
235224

236225
@Bean
237-
protected Step step1() throws Exception {
238-
return steps.get("step1").tasklet(new Tasklet() {
226+
protected Step step1(JobRepository jobRepository) throws Exception {
227+
return new StepBuilder("step1").repository(jobRepository).tasklet(new Tasklet() {
239228

240229
@Nullable
241230
@Override

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/JobLoaderConfigurationTests.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@
3232
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
3333
import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory;
3434
import org.springframework.batch.core.explore.JobExplorer;
35+
import org.springframework.batch.core.job.builder.JobBuilder;
3536
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
3637
import org.springframework.batch.core.launch.JobLauncher;
38+
import org.springframework.batch.core.repository.JobRepository;
3739
import org.springframework.batch.core.scope.context.ChunkContext;
40+
import org.springframework.batch.core.step.builder.StepBuilder;
3841
import org.springframework.batch.core.step.tasklet.Tasklet;
3942
import org.springframework.batch.repeat.RepeatStatus;
4043
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
@@ -122,28 +125,23 @@ public ApplicationObjectSupport fakeApplicationObjectSupport() {
122125
};
123126
}
124127

125-
@Autowired
126-
private JobBuilderFactory jobs;
127-
128-
@Autowired
129-
private StepBuilderFactory steps;
130-
131128
@Bean
132-
public Job testJob() throws Exception {
133-
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
129+
public Job testJob(JobRepository jobRepository) throws Exception {
130+
SimpleJobBuilder builder = new JobBuilder("test").repository(jobRepository).start(step1(jobRepository))
131+
.next(step2(jobRepository));
134132
return builder.build();
135133
}
136134

137135
@Bean
138-
protected Step step1() throws Exception {
139-
return steps.get("step1").tasklet(tasklet()).transactionManager(new ResourcelessTransactionManager())
140-
.build();
136+
protected Step step1(JobRepository jobRepository) throws Exception {
137+
return new StepBuilder("step1").repository(jobRepository).tasklet(tasklet())
138+
.transactionManager(new ResourcelessTransactionManager()).build();
141139
}
142140

143141
@Bean
144-
protected Step step2() throws Exception {
145-
return steps.get("step2").tasklet(tasklet()).transactionManager(new ResourcelessTransactionManager())
146-
.build();
142+
protected Step step2(JobRepository jobRepository) throws Exception {
143+
return new StepBuilder("step2").repository(jobRepository).tasklet(tasklet())
144+
.transactionManager(new ResourcelessTransactionManager()).build();
147145
}
148146

149147
@Bean
@@ -162,21 +160,15 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext context)
162160
@Configuration
163161
public static class VanillaConfiguration {
164162

165-
@Autowired
166-
private JobBuilderFactory jobs;
167-
168-
@Autowired
169-
private StepBuilderFactory steps;
170-
171163
@Bean
172-
public Job vanillaJob() throws Exception {
173-
SimpleJobBuilder builder = jobs.get("vanilla").start(step3());
164+
public Job vanillaJob(JobRepository jobRepository) throws Exception {
165+
SimpleJobBuilder builder = new JobBuilder("vanilla").repository(jobRepository).start(step3(jobRepository));
174166
return builder.build();
175167
}
176168

177169
@Bean
178-
protected Step step3() throws Exception {
179-
return steps.get("step3").tasklet(new Tasklet() {
170+
protected Step step3(JobRepository jobRepository) throws Exception {
171+
return new StepBuilder("step3").repository(jobRepository).tasklet(new Tasklet() {
180172
@Nullable
181173
@Override
182174
public RepeatStatus execute(StepContribution contribution, ChunkContext context) throws Exception {

0 commit comments

Comments
 (0)