How to start with Spring Batch using Spring Boot – Java Config

Many business operations need to process with batch job for critical environment. Spring Batch is a lightweight framework to boot the batch application. The tutorial will guide you how to start with Spring Batch using Spring Boot.

Related Articles:
How to start with Spring Batch using Spring Boot – XML Config
How to use Intercepting Job Execution in Spring Batch
How to use Spring Batch Restartable Function
Spring Batch – Programmatic Flow Decision
How to import CSV data to PostgreSQL Database using Spring Batch Job
How to configure Spring Batch Step for restart
How to use Spring Batch Late Binding – Step Scope & Job Scope
Spring Batch Partition for Scaling & Parallel Processing

I. Technologies for Spring Batch tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

II. Overview
1. Structure of project

spring batch spring boot - structure of project

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Configure datasource for Batch Job Repository
– Create Step: Reader, Processor, Writer
– Configure Batch Job
– Create a Controller for launch Job
– Run & Check result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info as below picture:

spring batch - input info

Press Next -> Finish, Spring Boot project is created.

2. Add needed dependencies

– Add dependencies: Web MVC, Spring Batch, PostgreSQL

3. Config datasource for Batch Job repository

Open application.propeties, add datasource info:

4. Create Step: Reader, Processor, Writer

– Create a simple Reader.java class: public interface ItemReader: is used to get data by read() method. The read() method is called repeatedly then returning a difference value for each time. If all data are consumed, a null value will be returned.

– Create a simple Processor.java class: public interface ItemProcessor is used for transforming data. We can provide input and output data type for each ItemProcessor via . A null returning is a finally signal of the processing.

– Create a simple Writer.java class: public interface ItemWriter is used to generate output of batch processing. The write() method get a list of items from batch reader (or batch processor) to process. The data can be persisted to database or other storages, or simple show on a console…

5. Enable Spring Batch

In main class, use @EnableBatchProcessing: Enable Spring Batch features and provide a base configuration for setting up batch jobs in an @Configuration class.

6. Configure Batch Job

Create a configuration file and enable Spring batch via annotations: @Configuration

7. Create a Controller for launch Job

Create a simple controller JobLauncherController with 1 @RequestMapping("/launchjob")

8. Run & Check result

– Maven build:
clean install

– Run project with mode: Spring Boot App
Make a request:
http://localhost:8080/launchjob

– Log:

See batch job tables in database:

spring batch - job tables

IV. Source code

SpringBatch

Related Article: How to start with Spring Batch using Spring Boot – XML Config



By grokonez | September 2, 2016.

Last updated on June 4, 2017.



Related Posts


4 thoughts on “How to start with Spring Batch using Spring Boot – Java Config”

  1. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘jobLauncherController’: Unsatisfied dependency expressed through field ‘job’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘job’ defined in class path resource [com/Batch/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method ‘job’ threw exception; nested exception is org.springframework.beans.factory.

    1. @Messi – Annotate JobLauncherController the with @EnableScheduling , UnsatisfiedDependencyException: should go away .. @EnableScheduling – autowires job launcher and some other features for free. for more info you can read on that annotation

  2. Can you explain how to read XML file and print it on console. I am a beginner to this Spring and I request you to provide me step by step procedure as shown in above example . Thanks in advance! Hoping a reply.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*