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 https://github.com/spring-projects/spring-batch/issues/4188
This commit is contained in:
Mahmoud Ben Hassine
2022-09-13 16:11:28 +02:00
parent 06c2dc3a01
commit 6e443cb1b1
45 changed files with 387 additions and 484 deletions

View File

@@ -24,9 +24,10 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
@@ -79,15 +80,9 @@ class JobLauncherTestUtilsTests {
@EnableBatchProcessing
static class TestJobConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Step step() {
return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
public Step step(JobRepository jobRepository) {
return new StepBuilder("step1").repository(jobRepository).tasklet(new Tasklet() {
@Nullable
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
@@ -97,14 +92,14 @@ class JobLauncherTestUtilsTests {
}
@Bean
public Job job() {
return jobBuilderFactory.get("job").flow(step()).end().build();
public Job job(JobRepository jobRepository) {
return new JobBuilder("job").repository(jobRepository).flow(step(jobRepository)).end().build();
}
@Bean
public JobLauncherTestUtils testUtils() {
public JobLauncherTestUtils testUtils(Job jobUnderTest) {
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(job());
jobLauncherTestUtils.setJob(jobUnderTest);
return jobLauncherTestUtils;
}

View File

@@ -28,10 +28,11 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
@@ -118,12 +119,6 @@ public class SpringBatchTestJUnit4Tests {
@EnableBatchProcessing
public static class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
@@ -149,9 +144,9 @@ public class SpringBatchTestJUnit4Tests {
}
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step")
public Job job(JobRepository jobRepository) {
return new JobBuilder("job").repository(jobRepository)
.start(new StepBuilder("step").repository(jobRepository)
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
.transactionManager(transactionManager(dataSource())).build())
.build();

View File

@@ -29,10 +29,11 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
@@ -143,9 +144,10 @@ public class SpringBatchTestJUnit5Tests {
}
@Bean
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step").tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
public Job job(JobRepository jobRepository) {
return new JobBuilder("job").repository(jobRepository)
.start(new StepBuilder("step").repository(jobRepository)
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
.transactionManager(transactionManager(dataSource())).build())
.build();
}

View File

@@ -31,9 +31,10 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
@@ -99,12 +100,6 @@ class StepScopeAnnotatedListenerIntegrationTests {
@EnableBatchProcessing
static class TestConfig {
@Autowired
private JobBuilderFactory jobBuilder;
@Autowired
private StepBuilderFactory stepBuilder;
@Autowired
private PlatformTransactionManager transactionManager;
@@ -127,13 +122,14 @@ class StepScopeAnnotatedListenerIntegrationTests {
}
@Bean
public Job jobUnderTest() {
return jobBuilder.get("job-under-test").start(stepUnderTest()).build();
public Job jobUnderTest(JobRepository jobRepository) {
return new JobBuilder("job-under-test").repository(jobRepository).start(stepUnderTest(jobRepository))
.build();
}
@Bean
public Step stepUnderTest() {
return stepBuilder.get("step-under-test").<String, String>chunk(1)
public Step stepUnderTest(JobRepository jobRepository) {
return new StepBuilder("step-under-test").repository(jobRepository).<String, String>chunk(1)
.transactionManager(this.transactionManager).reader(reader()).processor(processor())
.writer(writer()).build();
}