In Spring Batch, you implement an item reader by creating an ItemReader<T> bean, usually from a built-in class such as FlatFileItemReader or JdbcCursorItemReader, and an item writer by creating an ItemWriter<T> bean such as JdbcBatchItemWriter. You then plug both into a chunk-oriented step that reads items one at a time and writes them in chunks inside a single transaction. When no built-in class fits your source, you implement the interface yourself.
This guide uses the Spring Batch 5 style, which is also the base of Spring Boot 3 and later. In that style, you build jobs and steps with JobBuilder and StepBuilder and pass the JobRepository and a PlatformTransactionManager explicitly. The old JobBuilderFactory and StepBuilderFactory were removed.
A chunk-oriented step repeats this cycle until the reader returns null:
read() is called once per item until the chunk size is reached.ItemProcessor transforms or filters each item.write(Chunk<? extends T>) receives the whole chunk and writes it in one transaction.The commit interval is the chunk size. A failure rolls back only the current chunk, and the job repository records progress so a failed job can restart.
Use FlatFileItemReaderBuilder. It replaces the verbose anonymous-subclass setup that older examples show.
The name is required because the reader stores its position in the ExecutionContext under that key, which is what makes restarts work. With targetType, Spring Batch maps columns to a record or bean through a BeanWrapperFieldSetMapper (it supports records since version 5). Call .linesToSkip(1) if the file has a header row.
It keeps one cursor open and streams rows, which is memory-friendly for large tables:
JdbcPagingItemReader and JpaPagingItemReader run one query per page. They are thread-safe when configured correctly, so they suit multi-threaded steps, and they need a stable sort key. A cursor reader is not thread-safe.
JdbcBatchItemWriter sends the whole chunk as one JDBC batch. Named parameters can be bound from a bean or record with beanMapped():
Other common writers are FlatFileItemWriter, JpaItemWriter, KafkaItemWriter and CompositeItemWriter, which sends each chunk to several writers.
With Spring Boot, the job runs on startup by default. Set spring.batch.job.enabled=false if you want to launch it yourself with a JobLauncher. The Spring Batch metadata tables must exist, and Boot can create them with spring.batch.jdbc.initialize-schema.
Implement ItemReader<T> and return null when the data is exhausted. This reader pages through an in-memory list, but the same shape works for an API or queue:
Note that the writer receives a Chunk, not a List. This changed in Spring Batch 5, so older examples that use List<? extends T> will not compile. If your reader keeps state and should survive a restart, implement ItemStream as well, or extend AbstractItemCountingItemStreamItemReader. Spring Batch already provides a ListItemReader, so you rarely need to write one for plain lists.
Add .faultTolerant().skipLimit(10).skip(FlatFileParseException.class) to skip bad lines, or .retry(...) and .retryLimit(...) for transient errors such as deadlocks. Check the Spring Batch reference for the exact builder methods in the version you use, because the fault-tolerant API changed across 5.x and 6.x.
It signals the end of input. The step finishes after it writes the last, possibly smaller, chunk.
Start with 100 to 1,000 and measure. Larger chunks mean fewer commits but more memory and a bigger rollback when one item fails.
Cursor-based readers and FlatFileItemReader are not. Paging readers can be, but you should wrap other readers in SynchronizedItemStreamReader or use partitioning for parallel steps.
No. Writers always receive a whole Chunk. This is what lets JdbcBatchItemWriter use JDBC batching.
A Spring Batch step is a reader, an optional processor and a writer inside a transaction per chunk. Use the builders for FlatFileItemReader, JdbcCursorItemReader and JdbcBatchItemWriter, give each reader a unique name so it can restart, and write custom ItemReader and ItemWriter classes only when the built-in ones do not fit. Use StepBuilder, JobBuilder and Chunk in Spring Batch 5 and later.