Repository navigation
Description
Description
In Spring Batch 5, when using SimpleStepBuilder, I could implement an annotation-based ChunkListener by using @BeforeChunk / @AfterChunk / @AfterChunkError with methods that take ChunkContext as a parameter.
However, in Spring Batch 6, the same implementation triggers IllegalArgumentException.
In Spring Batch 6, I understand the situation as follows:
- Using
SimpleStepBuilderandChunkContextis deprecated. ChunkOrientedStepBuilderandChunkhave been introduced as replacements forSimpleStepBuilderandChunkContext.
Even so, I believe it is a bug that an annotation-based ChunkListener that takes ChunkContext as a parameter can no longer be implemented.
As far as I know, there are two ways to implement a ChunkListener: implementing the ChunkListener interface, and adding annotations to methods. Implementing the ChunkListener interface works in both versions of Spring Batch.
In other words, when implementing a ChunkListener under the following assumptions, Spring Batch 5 and Spring Batch 6 behave as shown below:
- Use
SimpleStepBuilder - Use
ChunkContextas the method parameter
Comparison table: ChunkListener behavior in Spring Batch 5 vs Spring Batch 6
| Spring Batch Version | Implement the ChunkListener interface |
Add annotations |
|---|---|---|
| Spring Batch 5 | Works | Works |
| Spring Batch 6 | Works | Fails(IllegalArgumentException) <- I believe this should work |
Code examples
Listener definition example:
@Component
public class ChunkLoggingListener {
private static final Logger logger = LoggerFactory.getLogger(ChunkLoggingListener.class);
@BeforeChunk
public void beforeChunk(ChunkContext context) {
logger.info("Annotation Logging: before chunk.");
}
@AfterChunk
public void afterChunk(ChunkContext context) {
logger.info("Annotation Logging: after chunk.");
}
@AfterChunkError
public void afterChunkError(ChunkContext context) {
Exception e = (Exception) context.getAttribute(ChunkListener.ROLLBACK_EXCEPTION_KEY);
logger.error("Annotation Logging: after chunk error.", e);
}
}Step definition example:
@Bean
public Step step01(JobRepository jobRepository,
@Qualifier("jobTransactionManager") PlatformTransactionManager transactionManager,
ListItemReader<Employee> employeeReader,
EmployeeProcessor employeeProcessor,
FlatFileItemWriter<Employee> employeeWriter,
ChunkLoggingListener listener) {
return new StepBuilder("job01.step01",
jobRepository)
.<Employee, Employee> chunk(10, transactionManager)
.listener(listener)
.reader(employeeReader)
.processor(employeeProcessor)
.writer(employeeWriter)
.build();
}Execution result
Failure case
`IllegalArgumentException` occurs when starting the job
[2026/02/09 18:20:29] [main] [o.h.v.i.util.Version ] [INFO ] HV000001: Hibernate Validator 9.1.0.Final
[2026/02/09 18:20:29] [main] [o.s.c.a.AnnotationConfigApplicationContext] [WARN ] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step01' defined in org.terasoluna.batch.tutorial.jobs.Job01Config: Failed to instantiate [org.springframework.batch.core.step.Step]: Factory method 'step01' threw exception with message: The method [afterChunk] on target class [ChunkLoggingListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterChunk].
[2026/02/09 18:20:29] [main] [o.s.b.c.l.s.CommandLineJobRunner] [ERROR] Job Terminated in error: Error creating bean with name 'step01' defined in org.terasoluna.batch.tutorial.jobs.Job01Config: Failed to instantiate [org.springframework.batch.core.step.Step]: Factory method 'step01' threw exception with message: The method [afterChunk] on target class [ChunkLoggingListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterChunk].
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step01' defined in org.terasoluna.batch.tutorial.jobs.Job01Config: Failed to instantiate [org.springframework.batch.core.step.Step]: Factory method 'step01' threw exception with message: The method [afterChunk] on target class [ChunkLoggingListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterChunk].
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1194)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:565)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:525)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:371)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:331)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.instantiateSingleton(DefaultListableBeanFactory.java:1218)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingleton(DefaultListableBeanFactory.java:1184)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:1121)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:993)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:620)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:92)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:307)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:584)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.step.Step]: Factory method 'step01' threw exception with message: The method [afterChunk] on target class [ChunkLoggingListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterChunk].
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:183)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:152)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 17 common frames omitted
Caused by: java.lang.IllegalArgumentException: The method [afterChunk] on target class [ChunkLoggingListener] is incompatible with the signature [(Chunk)] expected for the annotation [AfterChunk].
at org.springframework.util.Assert.isTrue(Assert.java:117)
at org.springframework.batch.infrastructure.support.MethodInvokerUtils.lambda$getMethodInvokerByAnnotation$0(MethodInvokerUtils.java:137)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:366)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:342)
at org.springframework.batch.infrastructure.support.MethodInvokerUtils.getMethodInvokerByAnnotation(MethodInvokerUtils.java:125)
at org.springframework.batch.core.listener.AbstractListenerFactoryBean.getObject(AbstractListenerFactoryBean.java:114)
at org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder.listener(AbstractTaskletStepBuilder.java:187)
at org.springframework.batch.core.step.builder.SimpleStepBuilder.listener(SimpleStepBuilder.java:264)
at org.terasoluna.batch.tutorial.jobs.Job01Config.step01(Job01Config.java:94)
at org.terasoluna.batch.tutorial.jobs.Job01Config$$SpringCGLIB$$0.CGLIB$step01$0(<generated>)
at org.terasoluna.batch.tutorial.jobs.Job01Config$$SpringCGLIB$$FastClass$$1.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:398)
at org.terasoluna.batch.tutorial.jobs.Job01Config$$SpringCGLIB$$0.step01(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:155)
... 20 common frames omitted
Expected result (in a working case, it looks like this)
[2026/02/09 18:31:54] [main] [o.h.v.i.util.Version ] [INFO ] HV000001: Hibernate Validator 8.0.2.Final
[2026/02/09 18:31:55] [main] [o.s.b.c.l.s.TaskExecutorJobLauncher] [INFO ] Job: [SimpleJob: [name=job01]] launched with the following parameters: [{'jsr_batch_run_id':'{value=1, type=class java.lang.Long, identifying=true}'}]
[2026/02/09 18:31:55] [main] [o.s.b.c.j.SimpleStepHandler] [INFO ] Executing step: [job01.step01]
[2026/02/09 18:31:55] [main] [o.t.b.t.l.ChunkLoggingListener] [INFO ] Annotation Logging: before chunk.
[2026/02/09 18:31:55] [main] [o.t.b.t.l.ChunkLoggingListener] [INFO ] Annotation Logging: after chunk.
[2026/02/09 18:31:55] [main] [o.s.b.c.s.AbstractStep] [INFO ] Step: [job01.step01] executed in 43ms
[2026/02/09 18:31:55] [main] [o.s.b.c.l.s.TaskExecutorJobLauncher] [INFO ] Job: [SimpleJob: [name=job01]] completed with the following parameters: [{'jsr_batch_run_id':'{value=1, type=class java.lang.Long, identifying=true}'}] and the following status: [COMPLETED] in 65ms
Environment where the error occurs
- Spring Batch 6.0.2
- Openjdk version "17.0.13" 2024-10-15 LTS
Activity
fmbenhassine commentedon Mar 17, 2026
We kept the v5 implementation in a deprecated form in v6 to give our users time to upgrade. So you could still use the annotations on methods accepting
ChunkContext, but in that case you should be using the v5 implementation.Otherwise you need to update your listener methods to the new contract, and use them with v6.
Please check #5226 and let me know if you need more support on this.