Skip to content

@BeforeStep/@AfterStep methods are ignored by ChunkOrientedStepBuilder#listener(Object) #5451

Closed

Description

@acktsap

I searched existing issues and found related listener-registration issues such as #5092, #5094, and #5347, but I could not find a duplicate for this specific case.

Bug description

ChunkOrientedStepBuilder#listener(Object) ignores annotation-based listeners that only declare @BeforeStep or @AfterStep methods.

The same object is registered if it also has another chunk/item listener annotation, such as @BeforeRead.

Environment

Spring Batch: 6.0.4
Java: 21

Steps to reproduce

Register a POJO with only a step-level listener annotation on a chunk-oriented step:

AtomicInteger called = new AtomicInteger();

Object listener = new Object() {

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        called.incrementAndGet();
    }

};

ChunkOrientedStep<String, String> step = new StepBuilder("step", new ResourcelessJobRepository())
        .<String, String>chunk(1)
        .reader(new ListItemReader<>(List.of("item")))
        .writer(chunk -> {
        })
        .listener(listener)
        .build();

step.execute(new StepExecution("step", new JobExecution(1L, new JobInstance(1L, "job"), new JobParameters())));

assertThat(called).hasValue(1); // currently 0

Expected behavior

@BeforeStep and @AfterStep methods should be detected by ChunkOrientedStepBuilder#listener(Object).

Minimal Complete Reproducible example

The snippet above reproduces the issue.

Activity

  1. added
    status: waiting-for-triage
    on Jul 7, 2026
  2. seonwooj0810 commented on Jul 15, 2026

    @seonwooj0810
    Contributor

    Would the team be open to a PR for this?

    The root cause looks self-contained: ChunkOrientedStepBuilder#listener(Object) scans for the chunk/item annotations (@BeforeChunk, @BeforeRead, @BeforeWrite, …) but not for the step-level @BeforeStep/@AfterStep, so a POJO declaring only those produces an empty method set and is never registered. StepBuilderHelper#listener(Object) already handles them (it adds BeforeStep.class/AfterStep.class to its scan), so this seems to be an inconsistency introduced by the new builder rather than an intentional change.

    If a fix is welcome, I'd add the two missing annotations to the scan and cover it with a regression test based on the reporter's reproducer.

  3. added and removed
    status: waiting-for-triage
    on Aug 13, 2026
  4. added this to the 6.0.6 milestone on Aug 13, 2026
  5. fmbenhassine commented on Aug 26, 2026

    @fmbenhassine
    Contributor

    Resolved with fa9ad1b.

  6. added
    for: backport-to-6.0.x
    on Sep 29, 2026
  7. modified the milestones: 6.0.6, 6.1.0-M2 on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: backport-to-6.0.xin: coretype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions