Spring Boot Integration with Spring Batch: A Comprehensive Tutorial
This article provides a step‑by‑step guide on integrating Spring Batch with Spring Boot, covering configuration, job and step setup, ItemReader/Processor/Writer implementations, database interactions, handling CSV and database sources, and troubleshooting common issues such as connection‑pool compatibility.
The article introduces Spring Batch as a robust batch‑processing framework built on Spring, highlighting its ease of integration, clear workflow, and features such as logging, transaction control, retry, and skip mechanisms.
Two typical business scenarios are presented: reading massive data from a CSV file and processing data from a database table, both followed by storage into a target table.
Project setup includes creating a MySQL table bloginfo and adding essential Maven dependencies for Spring Boot, Spring Batch, MyBatis, Druid, and validation libraries:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!-- other dependencies omitted for brevity -->The application.yml configures the datasource (initially using Druid) and disables automatic job launch:
spring:
batch:
job:
enabled: false
initialize-schema: always
datasource:
druid:
url: jdbc:mysql://localhost:3306/hellodemo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
...Domain model is defined by BlogInfo.java , a simple POJO with fields id , blogAuthor , blogUrl , blogTitle , and blogItem , together with getters, setters, and a toString method.
The MyBatis mapper BlogMapper.java provides insert and queryInfoById methods using annotations.
Batch configuration is centralized in MyBatchConfig.java . Key beans include:
JobRepository – registers jobs and handles transaction management.
SimpleJobLauncher – launches jobs.
Job – defines a job named myJob with a single step.
Step – composes ItemReader , ItemProcessor , and ItemWriter with chunk size 65000 and fault‑tolerant settings (retry 3 times, skip up to 2 failures).
Listeners MyJobListener , MyReadListener , and MyWriteListener log job lifecycle events and errors.
ItemReader – a FlatFileItemReader<BlogInfo> reads CSV rows and maps them to BlogInfo via a DelimitedLineTokenizer and BeanWrapperFieldSetMapper .
ItemProcessor – MyItemProcessor validates each item (using a custom MyBeanValidator ) and modifies the blogTitle based on the blogItem value.
ItemWriter – a JdbcBatchItemWriter<BlogInfo> inserts processed records into the database.
A REST controller TestController.java exposes /testJob to trigger the batch job with default parameters.
After the initial CSV‑based job, the article demonstrates a second scenario that reads data directly from the database using MyBatisCursorItemReader . A new job myJobNew and step stepNew are defined, along with a new processor MyItemProcessorNew that applies different title logic based on the numeric blogAuthor value.
Running the new job via /testJobNew?authorId=12345 initially fails because the Druid connection pool does not support the cursor reader. The solution is to remove the Druid dependency and rely on Spring Boot’s default HikariCP pool, after which the job executes successfully and writes transformed data into a new table bloginfonew .
The article concludes with a reminder to prefer HikariCP for performance, a brief recap of the batch components used, and links to related architecture articles.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.