Spring Batch Job Parameters Example

In this article we shall show you how you can pass job parameters to a spring batch job.

1. Introduction

A typical enterprise application requires a batch job that consists of one or more steps. Each step reads the input data, processes the input data, and writes the processed data to the configured output. In a batch job with more than one step, the output of a step is often used as an input of the next step.

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.

2. Basic concept

A Spring Batch job consists of the following components:

  1. A spring batch job combines one or more steps that belong logically together in a flow. All steps in a job are processed sequentially. A job has different job instances and these job instances differ on the basis of job parameters. JobParameters is a set of parameters used to start a batch job. JobParameters can be used for identification or even as reference data during the job run.They have reserved names, so to access them we can use Spring Expression Language. For example to access a property ‘abc’ on job parameters: we can access it using the syntax #{jobParameters[abc]}. A JobExecution has all the information about what happened during the job run.
  2. A Step represents an independent logical task.
  3. A JobLauncher represents a simple interface for launching a Job with a given set of JobParameters.
  4. An ItemReader reads the input data and provides the found items one by one.
  5. An ItemProcessor transforms the items into a form that is understood by the ItemWriter one item at a time.
  6. An ItemWriter writes an information of an item to the output one item at a time.

3. Sample Project

Let us see an example how we can pass and access JobParameters in a spring batch job.

3.1 Technologies used

I have used the following technologies to build this example.

  1. Spring sts 3.9
  2. JDK 1.8

In this project, I have a list of students in an XML format. I read it, I filter out the failed students i.e students with less than 30 marks, and I create a CSV file with all the passed students. I pass the name of the XML file and the CSV file using JobParameters to JobLauncher.

3.2 Project Structure

The following image shows the final project structure.

Fig.1 Project structure

3.3 Project Creation

This section shows how to create a Java-based Maven project in Spring Sts. In Spring Sts go to File-->New-->Maven Project.

Fig.2 Create maven project

In the New Maven Project window, enable ‘Use default workspace location‘ and ‘Create a simple project (skip archetype selection)‘ checkbox and click on the next button to proceed.

Fig.3 Project details

Enter the group details and the artifact id for the project as shown. The version number will be by default: 0.0.1-SNAPSHOT.

Fig.4 Archetype Selection

Click finish and a new maven project will be created.

3.4 Maven Dependencies

In the below xml file, I have defined the dependencies for spring batch, spring core and spring oxm.


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
        <!-- Spring Core -->
        		<!-- Spring XML to/back object -->

3.5 Java Class Creation

Let’s create the required Java files. Right-click on src/main/java folder, New -> Package.

Fig.5 Package creation

Enter the package name com.jcg.main in the popup window for package creation.

Fig.6 Package details

Following the above two steps for package creation, create two more packages com.jcg.model and com.jcg.processor.

Once the package is created let us create implementation classes as shown below. Right-click on the newly created package, New -> Class.

Fig.7 Java class creation

Enter classname as MainApplication. A new class will be created under the package com.jcg.main.

Fig.8 Java class details

Following the above steps for class creation, create a class for and under the package com.jcg.model and com.jcg.processor respectively.

Create a folder to hold the context and job related property files named config and an another folder xml to hold the xml file with a list of students as shown below.

Fig.9 Folder creation

Let us create the configuration files appcontext.xml and reportcard.xml for the project under config folder under src/main/resources.

Fig.10 File creation

Create an xml file StudentReport.xml with a list of all the students with their marks under folder xml under src/main/resources.

Below is the code for all the classes defined above.

In the below main class, I have passed the required JobParameters to the class JobLauncher alongwith the job.

package com.jcg.main;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;

public class MainApplication {

	public static void main(String[] args) {

		ApplicationContext context = 
				new ClassPathXmlApplicationContext("config/reportcard.xml","config/appcontext.xml");
		JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
		Job job = (Job) context.getBean("studentReportJob");

		try {
			JobParametersBuilder jobBuilder= new JobParametersBuilder();
			jobBuilder.addString("passstudentfilename", "PassedStudents");
			JobParameters jobParameters =jobBuilder.toJobParameters();
			JobExecution execution =, jobParameters);
			System.out.println("Completion Status : " + execution.getStatus());

		} catch (Exception e) {



The below model class has the required xml tags for marshalling and unmarshalling the xml objects into java objects.

package com.jcg.model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student")
public class Student {

		private int studentId;
		private String name;
		private int marks;

		@XmlAttribute(name = "studentId")
		public int getStudentId() {
			return studentId;

		public void setStudentId(int studentId) {
			this.studentId = studentId;

		@XmlElement(name = "name")
		public String getName() {
			return name;
		public void setName(String name) { = name;

		@XmlElement(name = "marks")
		public int getMarks() {
			return marks;

		public void setMarks(int marks) {
			this.marks = marks;


In the below processor class, I am filtering all the students who have scored less than 30 marks.

package com.jcg.processor;

import org.springframework.batch.item.ItemProcessor;

import com.jcg.model.Student;

public class PassedStudentReportProcessor implements ItemProcessor {

	public Student process(Student item) throws Exception {
		//filter object with marks< 30
		if(item.getMarks() <30){
			return null; // null = ignore this object
		return item;


The below xml file has definitions for JobRepository and TransactionManager.


<beans xmlns=""

	<bean id="jobRepository"
		<property name="transactionManager" ref="transactionManager" />
	<bean id="transactionManager"
		class="" />
	<bean id="jobLauncher"
		<property name="jobRepository" ref="jobRepository" />


The below xml file defines a job that reads an xml file, processes it and creates a csv file. Here, the xml filename and the csv filename are provided during runtime.


<beans xmlns=""
	xmlns:batch="" xmlns:task=""
	xmlns:util="" xmlns:xsi=""

	<batch:job id="studentReportJob">
		<batch:step id="step1">
				<batch:chunk reader="xmlItemReader" writer="cvsFileItemWriter" processor="passedStudentReportProcessor"

	<bean id="passedStudentReportProcessor" class="com.jcg.processor.PassedStudentReportProcessor" scope="step"/>

	<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
		<property name="fragmentRootElementName" value="student" />
		<property name="resource" value="classpath:xml/#{jobParameters['marksfileName']}.xml" />
		<property name="unmarshaller" ref="reportUnmarshaller" />

	<!-- Read and map values to object, via jaxb2 -->
	<bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
		<property name="classesToBeBound">

	<bean id="cvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">

		<!-- write to this csv file -->
		<property name="shouldDeleteIfExists" value="true" />
 		<property name="resource"
      value="file:csv/#{jobParameters['passstudentfilename']}.csv" />
		<property name="lineAggregator">
				<property name="delimiter" value="," />
				<property name="fieldExtractor">
						<property name="names" value="studentId,name,marks" />



The below xml file has a list of students with their respective marks.


<?xml version="1.0" encoding="UTF-8" ?>
    <student studentId="1001">
    <student studentId="1002">
    <student studentId="1003">

3.6 Maven Build

Build the application with command as Run As-->Maven Build--> Provide goals as clean package .

Fig.11 Maven build

3.7 Run Java Application

Run the application using the command Run As--> Java Application.

3.8 Project Demo

When I run the application as a java application, a PassedStudents.csv is created under the folder csv under the project alongside src folder with contents as shown in the below image.

Fig.12 CSV output

The above image has all the students that have marks greater than 30.

4. Conclusion

That’s all for getting the developers started with spring batch job parameters. I hope this article served you well. Developers can download the sample application as an Eclipse project in the Downloads section.

5. Download the Source Code

This was an example on how you can pass JobParameters to spring batch job.

You can download the full source code of this example here: Spring Batch Job Parameters Example

Garima Pandey

Garima is a software developer and have more than 7 years of experience in web application development using various java technologies with a passion to write quality code.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button