Spring Batch Hello World Example with Spring Boot latest, easy in 10 minutes

Today, I show you how to create a Spring Batch Hello World Example. This post introduces very clear to help you understand then you can create your own example. You know what, Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. Let’s dive into

Spring Batch Hello World Example

Project Setup

Tools and framework that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8

Project Directory

Our project will have a structure like below

Project Dependencies

We’re using the dependencies like below

Creating the Model

Here, we assume that we need to read employee information from CSV file named employees.csv, its format looks like below:

This file is located in the project folder resources /csv/employees.csv

Then, we create a POJO class that its object map with data in the above CSV file

Configuring Spring Batch Jobs

We assume that we have a data CSV file that contains a list of employees, its format looks like below.

Create BatchConfig class and add the @EnableBatchProcessing annotation for this class to get the support of Spring Batch features.

Create Spring Batch job that named myJob for our own Hello World application

Let’s dive into the above code:

The method helloWorldJob returns an instance of Jobbean. That Jobbean may be likely understood is a container of steps and other configuration options.

The methodhelloWorldStep returns an instance of Stepbean. This bean encapsulates phases of a batch job. Here, we have a step named myStep that takes care of reading and writing the CSV file.

We use the FlatFileItemReader to read the CSV file, its instance is created by the FlatFileItemReaderBuilder implementation. In order to FlatFileItemReader processes your CSV file, you must provide the information of file format, e.g: java mapping class, matched fields.

The EmployeeItemProcessor handles the processing of the data.

To write file we use FlatFileItemWriter that its instance created by a FlatFileItemWriterBuilder builder implementation.

Processing the Data

In most cases, we need to make some data processing from our original data. So we can create EmployeeItemProcessor that implements ItemProcessor interface to take care of that task. Let’s have a look at below implementation:

Configuring SpringBootApplication class

To configure Spring Boot application we create simple class below. If you haven’t had knowledge about Spring Boot yet. Let’s checkout the post Spring Boot Tutorial for Beginners

Running Application

To demo our Spring Batch Hello World Example, we run the below command in the console.

Note: You must be in your project directory before run that command.
While running that command, you will see the output like below

Spring Batch Hello World Example

That’s all about Spring Batch Hello World Example.

If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.


Spring Boot Tutorial for Beginners

Spring Batch Frameworks

Download the complete source code

SpringBatchHelloWorldExample-1.zip (208 downloads)
Please share it if you found this useful
Hide Buttons