Skip to content

Commit fb0d233

Browse files
committed
Fix incorrect default constructor in AbstractStep
This commit makes it impossible to instantiate AbstractStep without providing a job repository. Resolves #4984
1 parent a5eaffe commit fb0d233

File tree

19 files changed

+125
-55
lines changed

19 files changed

+125
-55
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowStep.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,17 +32,20 @@
3232
* parent and one each for the flow steps).
3333
*
3434
* @author Dave Syer
35+
* @author Mahmoud Ben Hassine
3536
*
3637
*/
3738
public class FlowStep extends AbstractStep {
3839

3940
private Flow flow;
4041

4142
/**
42-
* Default constructor convenient for configuration purposes.
43+
* Create a new instance of a {@link FlowStep} with the given job repository.
44+
* @param jobRepository the job repository to use. Must not be null.
45+
* @since 6.0
4346
*/
44-
public FlowStep() {
45-
super(null);
47+
public FlowStep(JobRepository jobRepository) {
48+
super(jobRepository);
4649
}
4750

4851
/**

spring-batch-core/src/main/java/org/springframework/batch/core/partition/PartitionStep.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.batch.core.job.JobExecutionException;
2121
import org.springframework.batch.core.observability.jfr.events.step.partition.PartitionAggregateEvent;
2222
import org.springframework.batch.core.observability.jfr.events.step.partition.PartitionSplitEvent;
23+
import org.springframework.batch.core.repository.JobRepository;
2324
import org.springframework.batch.core.step.Step;
2425
import org.springframework.batch.core.step.StepExecution;
2526
import org.springframework.batch.core.partition.support.DefaultStepExecutionAggregator;
@@ -45,6 +46,15 @@ public class PartitionStep extends AbstractStep {
4546

4647
private StepExecutionAggregator stepExecutionAggregator = new DefaultStepExecutionAggregator();
4748

49+
/**
50+
* Create a new instance of a {@link PartitionStep} with the given job repository.
51+
* @param jobRepository the job repository to use. Must not be null.
52+
* @since 6.0
53+
*/
54+
public PartitionStep(JobRepository jobRepository) {
55+
super(jobRepository);
56+
}
57+
4858
/**
4959
* A {@link PartitionHandler} which can send out step executions for remote processing
5060
* and bring back the results.

spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,33 @@ public abstract class AbstractStep implements StoppableStep, InitializingBean, B
7676
protected ObservationRegistry observationRegistry;
7777

7878
/**
79-
* Default constructor.
79+
* Create a new {@link AbstractStep}.
80+
* @deprecated since 6.0 for removal in 7.0. Use {@link #AbstractStep(JobRepository)}
81+
* instead.
8082
*/
83+
@Deprecated(since = "6.0", forRemoval = true)
8184
public AbstractStep() {
82-
super();
85+
}
86+
87+
/**
88+
* Create a new {@link AbstractStep}.
89+
* @deprecated since 6.0 for removal in 7.0. Use {@link #AbstractStep(JobRepository)}
90+
* instead.
91+
*/
92+
@Deprecated(since = "6.0", forRemoval = true)
93+
public AbstractStep(String name) {
94+
Assert.notNull(name, "Step name must not be null");
95+
this.name = name;
96+
}
97+
98+
/**
99+
* Create a new {@link AbstractStep} with the given job repository.
100+
* @param jobRepository the job repository. Must not be null.
101+
* @since 6.0
102+
*/
103+
public AbstractStep(JobRepository jobRepository) {
104+
Assert.notNull(jobRepository, "JobRepository must not be null");
105+
this.jobRepository = jobRepository;
83106
}
84107

85108
@Override
@@ -149,14 +172,6 @@ public void setAllowStartIfComplete(boolean allowStartIfComplete) {
149172
this.allowStartIfComplete = allowStartIfComplete;
150173
}
151174

152-
/**
153-
* Convenient constructor for setting only the name property.
154-
* @param name Name of the step
155-
*/
156-
public AbstractStep(String name) {
157-
this.name = name;
158-
}
159-
160175
/**
161176
* Extension point for subclasses to execute business logic. Subclasses should set the
162177
* {@link ExitStatus} on the {@link StepExecution} before returning.

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FlowStepBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
* nested flow composed of other steps.
2525
*
2626
* @author Dave Syer
27+
* @author Mahmoud Ben Hassine
2728
* @since 2.2
2829
*/
2930
public class FlowStepBuilder extends StepBuilderHelper<FlowStepBuilder> {
@@ -56,7 +57,7 @@ public FlowStepBuilder flow(Flow flow) {
5657
* @return a flow step
5758
*/
5859
public Step build() {
59-
FlowStep step = new FlowStep();
60+
FlowStep step = new FlowStep(getJobRepository());
6061
step.setName(getName());
6162
step.setFlow(flow);
6263
super.enhance(step);

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/JobStepBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* with parameters taken from the parent job or from the step execution.
2929
*
3030
* @author Dave Syer
31+
* @author Mahmoud Ben Hassine
3132
* @since 2.2
3233
*/
3334
public class JobStepBuilder extends StepBuilderHelper<JobStepBuilder> {
@@ -84,7 +85,7 @@ public JobStepBuilder parametersExtractor(JobParametersExtractor jobParametersEx
8485
*/
8586
public Step build() {
8687

87-
JobStep step = new JobStep();
88+
JobStep step = new JobStep(getJobRepository());
8889
step.setName(getName());
8990
super.enhance(step);
9091
if (job != null) {

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/PartitionStepBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -159,7 +159,7 @@ public PartitionStepBuilder aggregator(StepExecutionAggregator aggregator) {
159159
}
160160

161161
public Step build() {
162-
PartitionStep step = new PartitionStep();
162+
PartitionStep step = new PartitionStep(getJobRepository());
163163
step.setName(getName());
164164
super.enhance(step);
165165

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkOrientedStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ public class ChunkOrientedStep<I, O> extends AbstractStep {
162162
*/
163163
public ChunkOrientedStep(String name, int chunkSize, ItemReader<I> itemReader, ItemWriter<O> itemWriter,
164164
JobRepository jobRepository) {
165-
super(name);
165+
super(jobRepository);
166166
this.chunkSize = chunkSize;
167167
this.itemReader = itemReader;
168168
this.itemWriter = itemWriter;
169-
setJobRepository(jobRepository);
169+
setName(name);
170170
}
171171

172172
/**

spring-batch-core/src/main/java/org/springframework/batch/core/step/job/JobStep.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2008 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.batch.core.job.JobExecution;
2222
import org.springframework.batch.core.job.parameters.JobParameters;
2323
import org.springframework.batch.core.launch.JobOperator;
24+
import org.springframework.batch.core.repository.JobRepository;
2425
import org.springframework.batch.core.step.Step;
2526
import org.springframework.batch.core.step.StepExecution;
2627
import org.springframework.batch.core.job.UnexpectedJobExecutionException;
@@ -36,6 +37,7 @@
3637
* worker in a parallel or partitioned execution.
3738
*
3839
* @author Dave Syer
40+
* @author Mahmoud Ben Hassine
3941
*
4042
*/
4143
public class JobStep extends AbstractStep {
@@ -51,6 +53,15 @@ public class JobStep extends AbstractStep {
5153

5254
private JobParametersExtractor jobParametersExtractor = new DefaultJobParametersExtractor();
5355

56+
/**
57+
* Create a new instance of a {@link JobStep} with the given job repository.
58+
* @param jobRepository the job repository to use. Must not be null.
59+
* @since 6.0
60+
*/
61+
public JobStep(JobRepository jobRepository) {
62+
super(jobRepository);
63+
}
64+
5465
@Override
5566
public void afterPropertiesSet() throws Exception {
5667
super.afterPropertiesSet();

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,34 @@ public boolean rollbackOn(Throwable ex) {
103103

104104
/**
105105
* Default constructor.
106+
* @deprecated since 6.0 for removal in 7.0. Use {@link #TaskletStep(JobRepository)}
107+
* instead.
106108
*/
109+
@Deprecated(since = "6.0", forRemoval = true)
107110
public TaskletStep() {
108-
this(null);
111+
super();
109112
}
110113

111114
/**
115+
* Create a new instance with the given name.
116+
* @deprecated since 6.0 for removal in 7.0. Use {@link #TaskletStep(JobRepository)}
117+
* instead.
112118
* @param name the name for the {@link TaskletStep}
113119
*/
120+
@Deprecated(since = "6.0", forRemoval = true)
114121
public TaskletStep(String name) {
115122
super(name);
116123
}
117124

125+
/**
126+
* Create a new instance with the given name and job repository.
127+
* @param jobRepository the job repository to use. Must not be null.
128+
* @since 6.0
129+
*/
130+
public TaskletStep(JobRepository jobRepository) {
131+
super(jobRepository);
132+
}
133+
118134
@Override
119135
public void afterPropertiesSet() throws Exception {
120136
super.afterPropertiesSet();

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBeanTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler;
3434
import org.springframework.batch.core.step.JobRepositorySupport;
3535
import org.springframework.batch.core.step.StepSupport;
36-
import org.springframework.batch.core.step.builder.StepBuilderException;
3736
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
3837
import org.springframework.batch.core.step.tasklet.TaskletStep;
3938
import org.springframework.batch.item.ItemStream;
@@ -63,7 +62,7 @@ class StepParserStepFactoryBeanTests {
6362
@Test
6463
void testNothingSet() {
6564
StepParserStepFactoryBean<Object, Object> fb = new StepParserStepFactoryBean<>();
66-
assertThrows(StepBuilderException.class, fb::getObject);
65+
assertThrows(IllegalArgumentException.class, fb::getObject);
6766
}
6867

6968
@Test
@@ -98,7 +97,7 @@ void testSkipLimitSet() {
9897
StepParserStepFactoryBean<Object, Object> fb = new StepParserStepFactoryBean<>();
9998
fb.setName("step");
10099
fb.setSkipLimit(5);
101-
assertThrows(StepBuilderException.class, fb::getObject);
100+
assertThrows(IllegalArgumentException.class, fb::getObject);
102101
}
103102

104103
@Test

0 commit comments

Comments
 (0)