Skip to content

Commit bb2440f

Browse files
committed
Add ability to use bean names by default for jobs and steps
Resolves #4858
1 parent a7f090a commit bb2440f

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@
3131
public class JobBuilder extends JobBuilderHelper<JobBuilder> {
3232

3333
/**
34-
* Create a new builder for a job with the given name.
34+
* Create a new builder for a job with the given job repository. The name of the job
35+
* will be set to the bean name by default.
36+
* @param jobRepository the job repository to which the job should report to.
37+
* @since 6.0
38+
*/
39+
public JobBuilder(JobRepository jobRepository) {
40+
super(jobRepository);
41+
}
42+
43+
/**
44+
* Create a new builder for a job with the given name and job repository.
3545
* @param name the name of the job
3646
* @param jobRepository the job repository to which the job should report to
3747
* @since 5.0

spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public abstract class JobBuilderHelper<B extends JobBuilderHelper<B>> {
5454

5555
private final CommonJobProperties properties;
5656

57+
/**
58+
* Create a new {@link JobBuilderHelper}.
59+
* @param jobRepository the job repository
60+
* @since 6.0
61+
*/
62+
public JobBuilderHelper(JobRepository jobRepository) {
63+
this.properties = new CommonJobProperties();
64+
properties.jobRepository = jobRepository;
65+
}
66+
5767
/**
5868
* Create a new {@link JobBuilderHelper}.
5969
* @param name the job name
@@ -229,6 +239,8 @@ protected void enhance(AbstractJob job) {
229239

230240
public static class CommonJobProperties {
231241

242+
private String name;
243+
232244
private Set<JobExecutionListener> jobExecutionListeners = new LinkedHashSet<>();
233245

234246
private boolean restartable = true;
@@ -336,8 +348,6 @@ public void setRestartable(boolean restartable) {
336348
this.restartable = restartable;
337349
}
338350

339-
private String name;
340-
341351
}
342352

343353
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 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.
@@ -94,7 +94,6 @@ public AbstractStep() {
9494

9595
@Override
9696
public void afterPropertiesSet() throws Exception {
97-
Assert.state(name != null, "A Step must have a name");
9897
Assert.state(jobRepository != null, "JobRepository is mandatory");
9998
}
10099

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
*/
3535
public class StepBuilder extends StepBuilderHelper<StepBuilder> {
3636

37+
/**
38+
* Initialize a step builder for a step with the given job repository. The name of the
39+
* step will be set to the bean name by default.
40+
* @param jobRepository the job repository to which the step should report to.
41+
* @since 6.0
42+
*/
43+
public StepBuilder(JobRepository jobRepository) {
44+
super(jobRepository);
45+
}
46+
3747
/**
3848
* Initialize a step builder for a step with the given name and job repository.
3949
* @param name the name of the step

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public abstract class StepBuilderHelper<B extends StepBuilderHelper<B>> {
5353

5454
protected final CommonStepProperties properties;
5555

56+
/**
57+
* Create a new {@link StepBuilderHelper} with the given job repository.
58+
* @param jobRepository the job repository
59+
* @since 6.0
60+
*/
61+
public StepBuilderHelper(JobRepository jobRepository) {
62+
this.properties = new CommonStepProperties();
63+
properties.jobRepository = jobRepository;
64+
}
65+
5666
/**
5767
* Create a new {@link StepBuilderHelper}.
5868
* @param name the step name
@@ -176,6 +186,8 @@ protected void enhance(AbstractStep step) {
176186

177187
public static class CommonStepProperties {
178188

189+
private String name;
190+
179191
private List<StepExecutionListener> stepExecutionListeners = new ArrayList<>();
180192

181193
private int startLimit = Integer.MAX_VALUE;
@@ -272,8 +284,6 @@ public void setAllowStartIfComplete(Boolean allowStartIfComplete) {
272284
this.allowStartIfComplete = allowStartIfComplete;
273285
}
274286

275-
private String name;
276-
277287
}
278288

279289
}

0 commit comments

Comments
 (0)