From bb2440fea3a1a4685acba66261bebd49fa8c382d Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 3 Jun 2025 22:52:53 +0200 Subject: [PATCH] Add ability to use bean names by default for jobs and steps Resolves #4858 --- .../batch/core/job/builder/JobBuilder.java | 12 +++++++++++- .../batch/core/job/builder/JobBuilderHelper.java | 14 ++++++++++++-- .../batch/core/step/AbstractStep.java | 3 +-- .../batch/core/step/builder/StepBuilder.java | 10 ++++++++++ .../batch/core/step/builder/StepBuilderHelper.java | 14 ++++++++++++-- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java index 85f44a1a3..1d24c0e75 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java @@ -31,7 +31,17 @@ import org.springframework.batch.core.repository.JobRepository; public class JobBuilder extends JobBuilderHelper { /** - * Create a new builder for a job with the given name. + * Create a new builder for a job with the given job repository. The name of the job + * will be set to the bean name by default. + * @param jobRepository the job repository to which the job should report to. + * @since 6.0 + */ + public JobBuilder(JobRepository jobRepository) { + super(jobRepository); + } + + /** + * Create a new builder for a job with the given name and job repository. * @param name the name of the job * @param jobRepository the job repository to which the job should report to * @since 5.0 diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java index dee585863..f1a561394 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java @@ -54,6 +54,16 @@ public abstract class JobBuilderHelper> { private final CommonJobProperties properties; + /** + * Create a new {@link JobBuilderHelper}. + * @param jobRepository the job repository + * @since 6.0 + */ + public JobBuilderHelper(JobRepository jobRepository) { + this.properties = new CommonJobProperties(); + properties.jobRepository = jobRepository; + } + /** * Create a new {@link JobBuilderHelper}. * @param name the job name @@ -229,6 +239,8 @@ public abstract class JobBuilderHelper> { public static class CommonJobProperties { + private String name; + private Set jobExecutionListeners = new LinkedHashSet<>(); private boolean restartable = true; @@ -336,8 +348,6 @@ public abstract class JobBuilderHelper> { this.restartable = restartable; } - private String name; - } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java index c2339b95d..e3eeab20f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2024 the original author or authors. + * Copyright 2006-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,7 +94,6 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw @Override public void afterPropertiesSet() throws Exception { - Assert.state(name != null, "A Step must have a name"); Assert.state(jobRepository != null, "JobRepository is mandatory"); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java index 0f8f038f8..adef71809 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java @@ -34,6 +34,16 @@ import org.springframework.transaction.PlatformTransactionManager; */ public class StepBuilder extends StepBuilderHelper { + /** + * Initialize a step builder for a step with the given job repository. The name of the + * step will be set to the bean name by default. + * @param jobRepository the job repository to which the step should report to. + * @since 6.0 + */ + public StepBuilder(JobRepository jobRepository) { + super(jobRepository); + } + /** * Initialize a step builder for a step with the given name and job repository. * @param name the name of the step diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java index 802325a30..4ca46a5bf 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java @@ -53,6 +53,16 @@ public abstract class StepBuilderHelper> { protected final CommonStepProperties properties; + /** + * Create a new {@link StepBuilderHelper} with the given job repository. + * @param jobRepository the job repository + * @since 6.0 + */ + public StepBuilderHelper(JobRepository jobRepository) { + this.properties = new CommonStepProperties(); + properties.jobRepository = jobRepository; + } + /** * Create a new {@link StepBuilderHelper}. * @param name the step name @@ -176,6 +186,8 @@ public abstract class StepBuilderHelper> { public static class CommonStepProperties { + private String name; + private List stepExecutionListeners = new ArrayList<>(); private int startLimit = Integer.MAX_VALUE; @@ -272,8 +284,6 @@ public abstract class StepBuilderHelper> { this.allowStartIfComplete = allowStartIfComplete; } - private String name; - } }