The ItemWriter interface is the output side of a Spring Batch chunk-oriented step. After the reader and the optional processor have produced a chunk of items, the framework calls write(Chunk) once with the whole chunk, and the writer sends it to a database, a file, a message broker or another system. Writing a chunk at a time lets you batch inserts and commit once per chunk, which is far cheaper than one operation per item.
This article explains the contract (including the Spring Batch 5 change from List to Chunk), the built-in writers, transaction behaviour and how to write a custom one.
In Spring Batch 5 (Spring Boot 3) the interface is:
In Spring Batch 4 and earlier the parameter was List<? extends T>. Spring Batch 5 replaced it with Chunk, which wraps the items and also tracks skipped items and errors for fault-tolerant steps. A Chunk is iterable, and chunk.getItems() returns a List if you need one.
The writer receives only items that survived reading and processing. Items the processor filtered out by returning null never reach it.
For a step with a chunk size of 100, the framework repeats this cycle:
write() once with the resulting chunk.JobRepository.If write() throws, the transaction rolls back, so none of the chunk is committed. With a fault-tolerant step, Spring Batch can retry the chunk or scan it item by item to find and skip the bad record. That means a writer should be idempotent or at least safe to call again with the same items.
| Writer | Target | Notes |
|---|---|---|
JdbcBatchItemWriter | SQL inserts and updates | Uses JDBC batch statements |
JpaItemWriter | JPA entities | Calls persist or merge per item |
FlatFileItemWriter | CSV or text files | Needs a line aggregator |
JsonFileItemWriter | JSON array file | Uses Jackson or Gson |
StaxEventItemWriter | XML files | Streams elements |
RepositoryItemWriter | Spring Data repository | Calls a repository method per item |
KafkaItemWriter, JmsItemWriter | Messaging | Send items as messages |
CompositeItemWriter | Several writers | Writes the same chunk to many targets |
JdbcBatchItemWriter is usually the fastest choice for relational databases because it runs the whole chunk as one JDBC batch:
beanMapped() binds the named parameters from the item's properties, and assertUpdates(true) fails the chunk if a statement affects no rows. Use columnMapped() with a Map item instead if your items are maps.
The name is needed so the writer can store its position for restarts. The header callback writes the first line once.
Implement the interface when you must call an API or use a target the framework does not cover. Because it is a functional interface, a lambda also works for tiny cases.
For a real target, send the whole chunk in one call, for example a bulk API request, rather than looping and calling a remote service once per item. Do not catch and swallow exceptions inside the writer, because Spring Batch relies on the exception to roll back and to apply skip or retry rules.
Register it in the step with .writer(auditLogWriter), and combine several targets with CompositeItemWriter when the same chunk must go to a database and a file.
Once per chunk. With a chunk size of 100, the writer receives up to 100 items in one call, and the last chunk may be smaller.
The parameter changed from List<? extends T> to Chunk<? extends T>. Existing writers need that signature change when you upgrade.
The chunk transaction rolls back. The step fails, or retries and skips items if you configured a fault-tolerant step.
Yes. Use CompositeItemWriter to delegate the same chunk to several writers, or ClassifierCompositeItemWriter to route items to different writers by type.
ItemWriter receives a whole chunk from the framework and persists it to the target system inside the chunk transaction. In Spring Batch 5 the method takes a Chunk, not a List. Prefer built-in writers such as JdbcBatchItemWriter and FlatFileItemWriter, write in bulk, keep writers safe to retry, and let exceptions propagate so rollback, skip and retry work as designed.