Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Write the Java configuration for a Spring Batch 5 job with two steps: a chunk-oriented step that reads a CSV file and writes to a database, followed by a tasklet step that archives the file. Use Spring Boot auto-configuration.

With Spring Boot 3 and Spring Batch 5 we declare a Job bean with new JobBuilder("importJob", jobRepository).start(importStep).next(archiveStep).build(), a chunk step with new StepBuilder("importStep", jobRepository).<In, Out>chunk(100, transactionManager) and a reader, processor and writer, and a tasklet step with new StepBuilder("archiveStep", jobRepository).tasklet(tasklet, transactionManager). Boot supplies the JobRepository, transaction manager and job launcher, so we do not add @EnableBatchProcessing, which would switch that auto-configuration off.

Why this question is asked

This is the job most people write first in Spring Batch: read a CSV file, put the rows into a table, then move the file out of the way so it is not loaded again. It looks simple, but it touches every important idea: builders that need the job repository, chunks and their transactions, step-scoped beans that read job parameters, and the way Spring Boot launches the job. Interviewers ask for it because many examples online still use the removed JobBuilderFactory from Spring Batch 4, and it is easy to tell who has actually written a job on the current version. In this answer, we will recap the pieces, write the reader, processor and writer, build both steps and the job, see how Boot runs it, and note what changes in Spring Batch 6.

1A Quick Recap of the Building Blocks

A chunk-oriented step reads items one at a time, optionally transforms each one in a processor, and hands them to a writer in groups called chunks. Each chunk is written in one database transaction. With a chunk size of 100, Spring Batch reads 100 lines, processes them, writes 100 rows, and commits. A tasklet step is the other kind: it runs a single piece of code once, which suits jobs like moving a file.

In Spring Batch 5, the builders take the JobRepository directly. The JobRepository is the component that records each run in the batch tables. The old JobBuilderFactory and StepBuilderFactory were deprecated in version 5 and removed afterwards, so new code uses new JobBuilder(...) and new StepBuilder(...). The steps also take a PlatformTransactionManager, the Spring object that begins and commits transactions.

2The Reader, Processor and Writer

The reader parses the CSV file. We make it @StepScope, which means Spring creates it when the step starts rather than at application startup, so it can read the file path from the job parameters. The writer uses JDBC batch inserts, which send 100 inserts to the database in one round trip.

Java
public record CustomerCsv(long id, String name, String email) {}
public record Customer(long id, String name, String email) {}
@Bean
@StepScope
public FlatFileItemReader<CustomerCsv> customerReader(
@Value("#{jobParameters['file']}") String file) {
return new FlatFileItemReaderBuilder<CustomerCsv>()
.name("customerReader") // key for its saved position
.resource(new FileSystemResource(file))
.linesToSkip(1) // header line
.delimited()
.names("id", "name", "email")
.targetType(CustomerCsv.class) // records are supported
.build();
}
@Bean
public ItemProcessor<CustomerCsv, Customer> customerProcessor() {
return in -> new Customer(in.id(), in.name().trim(), in.email().toLowerCase());
}
@Bean
public JdbcBatchItemWriter<Customer> customerWriter(DataSource dataSource) {
return new JdbcBatchItemWriterBuilder<Customer>()
.dataSource(dataSource)
.sql("INSERT INTO customer (id, name, email) VALUES (?, ?, ?)")
.itemPreparedStatementSetter((c, ps) -> {
ps.setLong(1, c.id());
ps.setString(2, c.name());
ps.setString(3, c.email());
})
.build();
}

Notice the reader's name. Spring Batch stores the reader's position, the number of lines read, in the step's ExecutionContext under that name at every commit. If the job fails at line 60,000, a restart continues from the last committed chunk instead of the first line. The processor here only cleans values; returning null from it would drop an item.

3The Two Steps and the Job

Now we put the pieces together. The chunk step names its input and output types, In and Out in the question, which here are CustomerCsv and Customer. The tasklet step moves the file to an archive folder and returns FINISHED to say it is done.

Java
@Bean
public Step importStep(JobRepository jobRepository, PlatformTransactionManager tm,
FlatFileItemReader<CustomerCsv> customerReader,
ItemProcessor<CustomerCsv, Customer> customerProcessor,
JdbcBatchItemWriter<Customer> customerWriter) {
return new StepBuilder("importStep", jobRepository)
.<CustomerCsv, Customer>chunk(100, tm) // commit every 100 items
.reader(customerReader)
.processor(customerProcessor)
.writer(customerWriter)
.build();
}
@Bean
@StepScope
public Tasklet archiveTasklet(@Value("#{jobParameters['file']}") String file) {
return (contribution, chunkContext) -> {
Path src = Path.of(file);
Path dst = Path.of("/data/archive").resolve(src.getFileName());
Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING);
return RepeatStatus.FINISHED; // run once
};
}
@Bean
public Step archiveStep(JobRepository jobRepository, PlatformTransactionManager tm,
Tasklet archiveTasklet) {
return new StepBuilder("archiveStep", jobRepository)
.tasklet(archiveTasklet, tm)
.build();
}
@Bean
public Job importJob(JobRepository jobRepository, Step importStep, Step archiveStep) {
return new JobBuilder("importJob", jobRepository)
.start(importStep)
.next(archiveStep) // only runs if importStep completed
.build();
}
Step throughWhat importJob does
  1. Read 100 CSV linesFlatFileItemReader, position saved at commit
  2. Process each linetrim and lower-case
  3. Write 100 rows in one transactionJDBC batch insert, then commit
  4. More lines?
    YesRead the next chunkNoimportStep COMPLETED
  5. Move the file to the archivearchiveStep tasklet
  6. Job COMPLETEDrecorded in the job repository

6 steps in order. Play them, or step through them one at a time.

The order matters for safety. If the import fails, the job stops and archiveStep never runs, so the file stays where a restart can find it. Using REPLACE_EXISTING makes the move safe to repeat if the archive step itself is restarted.

4How Spring Boot Runs It

With spring-boot-starter-batch on the classpath and a DataSource configured, Boot creates the JobRepository, a JobLauncher and the transaction manager, and it can create the batch tables, controlled by spring.batch.jdbc.initialize-schema. At startup Boot also runs the job, passing command-line arguments of the form name=value as job parameters.

Properties
# application.properties
spring.batch.jdbc.initialize-schema=always # 'embedded' is the default
spring.batch.job.name=importJob # needed when there is more than one Job bean
# launch
java -jar import.jar file=/data/in/customers-2026-11-02.csv

Here is the catch that the question hints at. In Spring Boot 3, adding @EnableBatchProcessing, or defining our own bean extending DefaultBatchConfiguration, tells Boot that we want to configure batch ourselves, and its batch auto-configuration backs off. Then the schema is not initialised and the job is not launched at startup, which surprises people who copied the annotation from older examples. So we leave it out unless we really want full control.

5What Changes in Spring Batch 6

Spring Batch 6, which goes with Spring Boot 4, reorganised some packages, for example core classes such as Job and Step moved into more specific packages, and it simplified some builder methods. The chunk size can be given without a transaction manager, with the transaction manager set separately on the builder, and the default job repository setup changed as well. The ideas in this answer are the same, but the exact imports and signatures differ, so check your version's reference guide and migration notes when upgrading.

6Follow-up questions

How does Spring Boot decide which job to run at startup?

Boot's JobLauncherApplicationRunner runs at startup when spring.batch.job.enabled is true, which is the default. If the context contains exactly one Job bean, it runs that one. If there are several, Spring Boot 3 requires spring.batch.job.name to say which one, and fails at startup otherwise. The command-line arguments become job parameters, and if the job has an incrementer, Boot applies it to get the next parameters. For jobs launched by a scheduler or a REST call instead, we set spring.batch.job.enabled=false and call the JobLauncher ourselves.

Why is the reader @StepScope?

Because the file path is a job parameter, which only exists when the job is launched. A step-scoped bean is created when the step starts, so the late-binding expression #{jobParameters['file']} can be resolved. A side benefit is that each run gets a fresh reader, so state does not leak from one execution to the next.

Why use JdbcBatchItemWriter instead of a JPA repository's saveAll?

JdbcBatchItemWriter sends the whole chunk as one JDBC batch with no entity management, which is much faster for plain inserts. A JPA writer works, but needs hibernate.jdbc.batch_size to batch at all and adds persistence-context overhead per entity. For loading a file into a table, JDBC is the usual choice.

7Conclusion

In this answer, we wrote a two-step Spring Batch 5 job: a chunk-oriented importStep that reads a CSV with a step-scoped FlatFileItemReader, cleans each row and writes 100 rows per transaction with JdbcBatchItemWriter, followed by a tasklet archiveStep that moves the file. Both steps are built with new StepBuilder(name, jobRepository) and the job with new JobBuilder(name, jobRepository).start().next(). Spring Boot supplies the infrastructure and launches the job, as long as we do not add @EnableBatchProcessing, and Spring Batch 6 changes some packages and signatures.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write it with the Spring Batch 5 builders, which take the JobRepository directly. The reader is a FlatFileItemReader built with FlatFileItemReaderBuilder, delimited, mapped to a record, and marked @StepScope so it can take the file path from the job parameters with a late-binding expression.

Then, if they want more

A small ItemProcessor cleans the values, and the writer is a JdbcBatchItemWriter with an insert statement, so each chunk goes to the database as one JDBC batch. The import step is new StepBuilder("importStep", jobRepository), then chunk of 100 with the transaction manager, then reader, processor and writer.

The archive step is new StepBuilder("archiveStep", jobRepository).tasklet(...) with a tasklet that moves the file to an archive folder and returns RepeatStatus.FINISHED. The job is new JobBuilder("importJob", jobRepository).start(importStep).next(archiveStep).build(), so the file is only archived after a successful import.

With Spring Boot 3 I would not add @EnableBatchProcessing, because that turns off Boot's batch auto-configuration, including schema initialisation and running the job at startup. Boot runs the single job, or the one named in spring.batch.job.name, and takes parameters from the command line.

In Spring Batch 6 some packages and builder signatures changed, for example the chunk size without the transaction manager argument, so I would check the version.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Batch reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write the Java configuration for a Spring Batch 5 job with two steps: a chunk-oriented...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/78310d2a70/