Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch applications. As a Spring supplier, I’ve witnessed firsthand the transformative power of Spring Batch in streamlining batch processing operations across various industries. In this blog post, I’ll guide you through the process of creating a Spring Batch job, from understanding the basic concepts to implementing a complete job. Spring

Understanding Spring Batch Basics
Before diving into creating a Spring Batch job, it’s essential to understand the core concepts. Spring Batch consists of three main components: Jobs, Steps, and Tasklets.
A Job is an encapsulation of a batch process. It represents a complete unit of work and is composed of one or more Steps. Each Step is a discrete phase of the batch process, and it can be further divided into a Tasklet or a combination of a Reader, Processor, and Writer.
A Tasklet is a simple unit of work that performs a single operation, such as file deletion or database cleanup. On the other hand, the Reader, Processor, and Writer pattern is used for more complex operations, where the Reader reads data from a source, the Processor transforms the data, and the Writer writes the processed data to a destination.
Setting Up the Project
To create a Spring Batch job, you first need to set up a Spring Boot project. You can use Spring Initializr (start.spring.io) to generate a basic project with the necessary dependencies. For a Spring Batch project, you’ll need the Spring Batch and Spring Boot Starter Data JPA dependencies.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
In this example, we’re using H2 as an in-memory database for simplicity. You can replace it with your preferred database, such as MySQL or PostgreSQL.
Defining the Job
Once the project is set up, you can start defining the Spring Batch job. First, create a configuration class to define the job and its steps.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
public BatchConfig(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
}
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.start(myStep())
.build();
}
@Bean
public Step myStep() {
return stepBuilderFactory.get("myStep")
.tasklet((contribution, chunkContext) -> {
System.out.println("Executing my step");
return RepeatStatus.FINISHED;
}).build();
}
}
In this code, we’ve defined a simple job named myJob that consists of a single step named myStep. The step is a Tasklet that simply prints a message to the console.
Running the Job
To run the Spring Batch job, you can create a main class that extends SpringBatchApplication and use the JobLauncher to launch the job.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BatchApplication implements CommandLineRunner {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job myJob;
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(myJob, jobParameters);
}
}
In this code, we’re using the JobLauncher to launch the myJob with some job parameters. The job parameters are used to uniquely identify each execution of the job.
Using Reader, Processor, and Writer
For more complex batch processing, you can use the Reader, Processor, and Writer pattern. Let’s assume we have a simple CSV file with user data, and we want to read the data from the file, process it, and write it to a database.
First, create a model class to represent the user data.
public class User {
private String name;
private int age;
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Next, create a Reader to read the data from the CSV file.
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.ClassPathResource;
public class UserItemReader extends FlatFileItemReader<User> {
public UserItemReader() {
setResource(new ClassPathResource("users.csv"));
setLineMapper(new DefaultLineMapper<User>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"name", "age"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() {{
setTargetType(User.class);
}});
}});
}
}
Then, create a Processor to transform the data.
import org.springframework.batch.item.ItemProcessor;
public class UserItemProcessor implements ItemProcessor<User, User> {
@Override
public User process(User user) throws Exception {
// Simple transformation: uppercase the name
user.setName(user.getName().toUpperCase());
return user;
}
}
Finally, create a Writer to write the processed data to the database.
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class UserItemWriter extends JdbcBatchItemWriter<User> {
public UserItemWriter(DataSource dataSource) {
setDataSource(dataSource);
setSql("INSERT INTO users (name, age) VALUES (:name, :age)");
setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
}
}
Now, update the BatchConfig class to use the Reader, Processor, and Writer.
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
private final DataSource dataSource;
public BatchConfig(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, DataSource dataSource) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.dataSource = dataSource;
}
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.start(myStep())
.build();
}
@Bean
public Step myStep() {
return stepBuilderFactory.get("myStep")
.<User, User>chunk(10)
.reader(userItemReader())
.processor(userItemProcessor())
.writer(userItemWriter())
.build();
}
@Bean
public ItemReader<User> userItemReader() {
return new UserItemReader();
}
@Bean
public ItemProcessor<User, User> userItemProcessor() {
return new UserItemProcessor();
}
@Bean
public ItemWriter<User> userItemWriter() {
return new UserItemWriter(dataSource);
}
}
Monitoring and Error Handling
Spring Batch provides built-in support for monitoring and error handling. You can use the JobExecution and StepExecution objects to track the progress of the job and handle errors.
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;
@Component
public class MyJobExecutionListener implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("Job started: " + jobExecution.getJobInstance().getJobName());
}
@Override
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus().isUnsuccessful()) {
System.out.println("Job failed: " + jobExecution.getJobInstance().getJobName());
} else {
System.out.println("Job completed: " + jobExecution.getJobInstance().getJobName());
}
}
}
You can then register the listener with the job in the BatchConfig class.
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.listener(myJobExecutionListener())
.start(myStep())
.build();
}
@Bean
public MyJobExecutionListener myJobExecutionListener() {
return new MyJobExecutionListener();
}
Conclusion

Creating a Spring Batch job involves understanding the core concepts, setting up the project, defining the job and its steps, and implementing the necessary components such as Reader, Processor, and Writer. With Spring Batch, you can easily build robust batch applications that can handle large volumes of data efficiently.
Exciter As a Spring supplier, we have the expertise and experience to help you create and optimize your Spring Batch jobs. Whether you’re looking to streamline your existing batch processes or develop new ones, we’re here to assist you. If you’re interested in learning more about our Spring Batch solutions or have any questions, please don’t hesitate to contact us for a procurement discussion.
References
- Spring Batch Documentation
- Spring Boot Documentation
- Java Persistence API (JPA) Documentation
Xinxiang Fengda Machinery Co., Ltd.
We’re well-known as one of the leading spring manufacturers and suppliers in China, specialized in providing high quality customized service for global clients. We warmly welcome you to buy high-grade spring made in China here from our factory.
Address: No.16 Wangguanying Village, Kangcun Town, Huojia County, Xinxiang City, Henan Province, China
E-mail: xxfdjx@163.com
WebSite: https://www.flipflowscreen.com/