From 9df26d78282416e367f5d38e2ef660730f881b1a Mon Sep 17 00:00:00 2001 From: Nikita Nagar Date: Sat, 7 Mar 2026 21:25:33 +0530 Subject: [PATCH] Introduce addListener method in StepBuilder and JobBuilder for better clarity Fixes #5321 Add addListener() methods as aliases that delegate to the existing listener() methods. The new method names make the additive behavior of adding listeners explicit and improve API consistency. Changes: - StepBuilderHelper.addListener(StepExecutionListener) - delegates to listener() - StepBuilderHelper.addListener(Object) - delegates to listener() for annotation-based config - JobBuilderHelper.addListener(JobExecutionListener) - delegates to listener() - JobBuilderHelper.addListener(Object) - delegates to listener() for annotation-based config The existing listener() methods remain for backward compatibility. Signed-off-by: Nikita Nagar --- .../core/job/builder/JobBuilderHelper.java | 22 +++++++++++++++++++ .../core/step/builder/StepBuilderHelper.java | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) 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 54d7859310..b5ef7203ec 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 @@ -154,6 +154,28 @@ public B listener(JobExecutionListener listener) { return result; } + /** + * Add a {@link JobExecutionListener} to the job. + * This method makes the additive nature of adding listeners explicit. + * @param listener the job execution listener to add + * @return this for fluent chaining + * @since 6.1 + */ + public B addListener(JobExecutionListener listener) { + return listener(listener); + } + + /** + * Add a job execution listener using annotation-based configuration. + * This method makes the additive nature of adding listeners explicit. + * @param listener the object that has a method configured with listener annotation + * @return this for fluent chaining + * @since 6.1 + */ + public B addListener(Object listener) { + return listener(listener); + } + /** * Set a flag to prevent restart an execution of this job even if it has failed. * @return this to enable fluent chaining 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 da00172ffe..86434afca4 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 @@ -117,6 +117,28 @@ public B listener(StepExecutionListener listener) { return self(); } + /** + * Add a {@link StepExecutionListener} to the step. + * This method makes the additive nature of adding listeners explicit. + * @param listener the step execution listener to add + * @return this for fluent chaining + * @since 6.1 + */ + public B addListener(StepExecutionListener listener) { + return listener(listener); + } + + /** + * Add a step execution listener using annotation-based configuration. + * This method makes the additive nature of adding listeners explicit. + * @param listener the object that has a method configured with listener annotation + * @return this for fluent chaining + * @since 6.1 + */ + public B addListener(Object listener) { + return listener(listener); + } + public B allowStartIfComplete(boolean allowStartIfComplete) { properties.allowStartIfComplete = allowStartIfComplete; return self();