EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Batch Example
 

Spring Batch Example

Updated March 30, 2023

Spring Batch Example

 

 

Definition of Spring Batch Example

Spring Batch example is a feature-rich framework that makes it easier to create reliable batch applications. Spring batch task implementation contains multiple steps, each of which processes, reads, and writes data. Spring batch is a lightweight, all-in-one framework that enables the creation of reliable batch applications which was critical to the day-to-day operations of systems. Spring batch architecture uses a job repository for scheduling the new jobs and communicating with existing jobs.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Examples

The below example shows steps to create spring batch example are as follows.

1) Create project template by using spring initializer

  • In the below step we have provided project group name as com. example, artifact name as SpringBatchExample, project name as SpringBatchExample, and selected java version as 8. Also, we have defined spring boot version as 2.6.0, a defined project as maven.
  • In the below project, we have selected spring web, spring batch, and PostgreSQL driver dependency to implement the spring batch project.

Group – com. example Artifact name – SpringBatchExample
Name – SpringBatchExample Spring boot – 2.6.0
Project – Maven Java – 8
Package name – com.example.SpringBatchExample
Project Description – Project for SpringBatchExample
Dependencies – spring web, PostgreSQL driver, spring batch.

1

2) After generating project extract files and open this project by using tool suite –

After generating the project by using the initializer in this step we are extracting the jar file and opening the project by using the tool suite.

2

3) After opening project using tool suite check the project and its files –

  • In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.

3

4) Add dependency packages –

  • In this step, we are adding the batch dependency in the project.

Code –

<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-batch</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.springframework.batch</groupId> -- Start and end of groupId tag.
<artifactId>spring-batch-test</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.postgresql</groupId> -- Start and end of groupId tag.
<artifactId>postgresql</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.

4

5) Create CSV file for spring batch processing –

  • In this step, we are creating CSV file for batch processing.

Code:

101,ABC,
202, PQR,
303, XYZ

5

6) Create database for database and tables connection –

  • In this step, we are creating database name as springbatchexample and table name as stud for database connection are as follows.

Code:

# create database springbatchexample;
# \l+ springbatchexample
# \c springbatchexample
# create table stud (stud_id int, stud_name varchar(5));
# \d+ stud;

6

7) Configure application.properties file –

  • In this step, we are configuring the application.properties file. We are adding a database connection in our spring batch example project. We are using PostgreSQL database to add connections in the spring batch project.

Code:

spring.application.name = SpringBatchExample
spring.datasource.url = jdbc:postgresql://localhost:5432/springbatchexample
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.batch.initialize-schema = always

7

8) Create an entity class to process the data –

  • In this step, we are creating the stud entity class to process the data. We have used the entity name stud_id and stud_name.
  • As we have seen in the below example we have used stud_id and stud_name entities.

Code:

@Data
@Entity
@Table(catalog="springbatchexample", name = "stud")
public class stud {
@Id
private long stud_id;
private String stud_name;
}

8

9) Enable batch processing in the project –

  • In this step, we have enabled the spring batch processing of our spring boot example project.
  • We are using @EnableBatchProcessing annotation to process the batch. This annotation is an enabled a feature of the spring batch in our project.

Code:

@SpringBootApplication
@EnableBatchProcessing
public class EnableSpringBatch {
public static void main /* main method of spring batch example project */ (String[] args) {
SpringApplication.run (EnableSpringBatch.class, args);
}
}

9

10) Configure batch using JobBuilderFactory method –

  • In this step, we have configuring batch of our spring boot example project by using JobBuilderFactory method.
  • We have created springConfig class to configure batch in spring batch example project.

Code:

@Configuration
public class springConfig {
@Autowired
private JobBuilderFactory JBF;
@Autowired
private StepBuilderFactory SBF;
}

10

11) Config the item reader for our spring batch example project –

  • In this step we have configuring the item reader for our spring batch example project are as follows.
  • To configure item reader, we are creating bean by using FlatFileItemReader are as follows.

Code:

<batch:job idc = "springbatch">
<batch:step id = "1">
<batch:tasklet>
<batch:chunk reader = "reader" writer="writer"
Processor = "processor" commit-interval = "1">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>

12

12) Create spring batch model class –

  • In this step we are creating spring batch model class are as follows.

Code:

public class batchModel {
private int stud_id;
private String stud_name;
public int getstudId () {
return stud_id;
}
public void setId(int studid) {
this.stud_id = studid;
}
public void setstudName (String studname) {
this.stud_name = studname;
}
}

Spring batch example 13

13) Define spring batch processor –

  • In this step we have defined the batch processor of our spring batch application are as follows.

Code:

public class Processor implements ItemProcessor {
public batchModel process(batchModel stud) throws Exception {
System.out.println ("spring batch processing" + stud);
String fname = stud.stud_id ();
String lname = stud.stud_name ();
stud.setstud_id (stud_id.toUpperCase ());
stud.setstud_name (stud_name.toUpperCase ());
return stud;
}
@Override
public Object processItem(Object stud) throws Exception {
return null;
}
}

Spring batch example 14

14) Create spring batch test program –

  • In this step we are creating spring batch test program are as follows.

Code:

public class springbatch {
String [] springConfig = {"springbatchexample/jobs/SpringBatch.xml" };
try {
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Completed");
context.close();
}
}

Spring batch example 15

15) Run the application –

Spring batch example 16

Conclusion

In this article, we have provided a step-by-step implementation of the spring batch example. Spring batch is a lightweight, all-in-one framework that enables the creation of reliable batch applications which was critical to the day-to-day operations of systems.

Recommended Articles

This is a guide to Spring batch example. Here we discuss the definition, steps of Spring Batch examples with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Batch Architecture
  2. Spring Batch Interview Questions
  3. Spring Boot Batch
  4. Spring Boot Docker

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW