Backend Development 22 min read

Integrating Spring Batch with Spring Boot: Full Tutorial and Example

This article provides a comprehensive step‑by‑step guide on integrating Spring Batch with Spring Boot, covering configuration, entity and mapper definitions, job, step, reader, processor, writer implementations, database setup, error handling, and execution via a REST controller, complete with code examples.

Top Architect
Top Architect
Top Architect
Integrating Spring Batch with Spring Boot: Full Tutorial and Example

The article introduces Spring Batch as a robust, easy‑to‑use batch processing framework built on Spring, highlighting its advantages such as simple integration, clear workflow, logging, transaction management, retry, and skip mechanisms.

Two typical business scenarios are presented: reading large volumes of data from a CSV file and from a database table, processing the data, and persisting the results.

A MySQL table bloginfo is created with a corresponding SQL script, and essential Maven dependencies (Spring Boot starter web, test, batch, Hibernate validator, MyBatis, MySQL driver, Druid datasource) are listed.

The core configuration class MyBatchConfig is explained in detail. It defines:

JobRepository bean for job metadata storage.

JobLauncher bean to launch jobs.

Job bean with a RunIdIncrementer and a listener.

JobListener implementation ( MyJobListener ) that logs job start and end.

Step bean that composes ItemReader, ItemProcessor, and ItemWriter, with fault‑tolerant settings (retry limit, skip limit) and associated listeners.

ItemReader reads CSV data using FlatFileItemReader<BlogInfo> with a line mapper that maps columns to the BlogInfo POJO.

FlatFileItemReader<BlogInfo> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("static/bloginfo.csv"));
reader.setLineMapper(new DefaultLineMapper<BlogInfo>(){
    {
        setLineTokenizer(new DelimitedLineTokenizer(){
            {setNames("blogAuthor","blogUrl","blogTitle","blogItem");}
        });
        setFieldSetMapper(new BeanWrapperFieldSetMapper<BlogInfo>(){
            {setTargetType(BlogInfo.class);}
        });
    }
});

ItemProcessor extends ValidatingItemProcessor<BlogInfo> to apply a custom validator and performs simple business logic: if blogItem equals "springboot" the title is set to a specific value, otherwise it defaults to "未知系列".

public BlogInfo process(BlogInfo item) throws ValidationException {
    super.process(item);
    if (item.getBlogItem().equals("springboot")) {
        item.setBlogTitle("springboot 系列还请看看我Jc");
    } else {
        item.setBlogTitle("未知系列");
    }
    return item;
}

The validator MyBeanValidator uses JSR‑303 to validate entities and throws a ValidationException when constraints are violated.

ItemWriter uses JdbcBatchItemWriter<BlogInfo> with a parameterized SQL insert statement to persist processed records into the bloginfo table.

String sql = "insert into bloginfo (blogAuthor,blogUrl,blogTitle,blogItem) values(:blogAuthor,:blogUrl,:blogTitle,:blogItem)";
writer.setSql(sql);
writer.setDataSource(dataSource);

Read, process, and write listeners ( MyReadListener and MyWriteListener ) log before/after actions and errors.

A REST controller ( TestController ) exposes an endpoint /testJob that builds JobParameters and triggers the batch job via the SimpleJobLauncher .

The article then extends the example to a second job that reads data from the database using MyBatisCursorItemReader , processes it with a new processor ( MyItemProcessorNew ), and writes to a new table bloginfonew . It discusses connection‑pool issues with Druid and recommends using Spring Boot's default HikariCP.

Finally, the article provides execution screenshots, results verification, and additional resources, emphasizing that Spring Batch offers powerful transaction control, restartability, and fault tolerance for large‑scale data processing tasks.

Javabatch processingSpring BootMySQLMyBatisSpring Batch
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.