
- Spring Batch - Home
- Spring Batch - Overview
- Spring Batch - Environment
- Spring Batch - Architecture
- Spring Batch - Application
- Spring Batch - Configuration
- Readers, Writers & Processors
- Spring Batch - Basic Application
- Spring Batch - XML to MySQL
- Spring Batch - CSV to XML
- Spring Batch - MySQL to XML
- Spring Batch - MySQL to Flat File
Spring Batch - Resources
Spring Batch - Basic Application
This chapter shows you the basic Spring Batch application. It will simply execute a tasklet to displays a message.
Our Spring Batch application contains the following files −
Configuration file − This is an XML file where we define the Job and the steps of the job. (If the application involves readers and writers too, then the configuration of readers and writers is also included in this file.)
Context.xml − In this file, we will define the beans like job repository, job launcher and transaction manager.
Tasklet class − In this class, we will write the processing code job (In this case, it displays a simple message)
Launcher class − in this class, we will launch the Batch Application by running the Job launcher.
Create Project
Create a new maven project as discussed in Spring Batch - Environment Chapter.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialspoint</groupId> <artifactId>SpringBatch</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBatchExample</name> <properties> <java.version>24</java.version> <spring.version>7.0.0-M9</spring.version> <spring.batch.version>6.0.0-M3</spring.batch.version> <mysql.driver.version>9.4.0</mysql.driver.version> <junit.version>4.11</junit.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Batch dependencies --> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>${spring.batch.version}</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> <!-- Spring Batch unit test --> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <version>${spring.batch.version}</version> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- MySql Connector--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>${mysql.driver.version}</version> </dependency> </dependencies> </project>
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. Create this file in src > main > resources folder of maven project.
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:batch = "http://www.springframework.org/schema/batch" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <import resource="context.xml" /> <!-- Defining a bean --> <bean id = "tasklet" class = "com.tutorialspoint.MyTasklet" /> <!-- Defining a job--> <batch:job id = "helloWorldJob"> <!-- Defining a Step --> <batch:step id = "step1"> <tasklet ref = "tasklet"/> </batch:step> </batch:job> </beans>
Context.xml
Following is the context.xml of our Spring Batch application. Create this file in src > main > resources folder of maven project.
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:jdbc = "http://www.springframework.org/schema/jdbc" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd"> <!-- stored job-meta in database --> <bean id = "jobRepository" class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> <property name = "dataSource" ref = "dataSource" /> <property name = "transactionManager" ref = "transactionManager" /> <property name = "databaseType" value = "mysql" /> </bean> <bean id = "transactionManager" class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> <bean id = "jobLauncher" class = "org.springframework.batch.core.launch.support.TaskExecutorJobLauncher"> <property name = "jobRepository" ref = "jobRepository" /> </bean> <!-- connect to MySQL database --> <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name = "url" value = "jdbc:mysql://localhost:3306/details" /> <property name = "username" value = "guest" /> <property name = "password" value = "guest123" /> </bean> <!-- create job-meta tables automatically --> <jdbc:initialize-database data-source = "dataSource"> <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql"/> <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql"/> </jdbc:initialize-database> </beans>
Tasklet.java
Following is the Tasklet class which displays a simple message. Create this class in src > main > java folder of maven project.
package com.tutorialspoint; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.StepContribution; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; public class MyTasklet implements Tasklet { @Override public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { System.out.println("Hello This is a sample example of spring batch"); return RepeatStatus.FINISHED; } }
App.java
Following is the code which launces the batch process. Create this class in src > main > java folder of maven project.
package com.tutorialspoint; import org.springframework.batch.core.job.Job; import org.springframework.batch.core.job.JobExecution; import org.springframework.batch.core.job.parameters.JobParameters; import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args)throws Exception { String[] springConfig = {"jobConfig.xml"}; // Creating the application context object ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); // Creating the job launcher TaskExecutorJobLauncher jobLauncher = (TaskExecutorJobLauncher) context.getBean("jobLauncher"); // Creating the job Job job = (Job) context.getBean("helloWorldJob"); // Executing the JOB JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + execution.getStatus()); } }
Output
Right click on the project in eclipse, select run as -> maven build . Set goals as clean package and run the project. You'll see following output.
[INFO] Scanning for projects... [INFO] [INFO] [1m-------------------< [0;36mcom.tutorialspoint:SpringBatch[0;1m >-------------------[m [INFO] [1mBuilding SpringBatchExample 0.0.1-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m--------------------------------[ jar ]---------------------------------[m [INFO] [INFO] [1m--- [0;32mclean:3.2.0:clean[m [1m(default-clean)[m @ [36mSpringBatch[0;1m ---[m [INFO] Deleting C:\Users\mahes\eclipse-workspace\SpringBatch\target [INFO] [INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mSpringBatch[0;1m ---[m [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources from src\main\resources to target\classes [INFO] [INFO] [1m--- [0;32mcompiler:3.7.0:compile[m [1m(default-compile)[m @ [36mSpringBatch[0;1m ---[m [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 2 source files to C:\Users\mahes\eclipse-workspace\SpringBatch\target\classes [INFO] /C:/Users/mahes/eclipse-workspace/SpringBatch/src/main/java/com/tutorialspoint/App.java: C:\Users\mahes\eclipse-workspace\SpringBatch\src\main\java\com\tutorialspoint\App.java uses or overrides a deprecated API that is marked for removal. [INFO] /C:/Users/mahes/eclipse-workspace/SpringBatch/src/main/java/com/tutorialspoint/App.java: Recompile with -Xlint:removal for details. [INFO] [INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mSpringBatch[0;1m ---[m [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource from src\test\resources to target\test-classes [INFO] [INFO] [1m--- [0;32mcompiler:3.7.0:testCompile[m [1m(default-testCompile)[m @ [36mSpringBatch[0;1m ---[m [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] [1m--- [0;32msurefire:3.2.5:test[m [1m(default-test)[m @ [36mSpringBatch[0;1m ---[m [INFO] [INFO] [1m--- [0;32mjar:3.4.1:jar[m [1m(default-jar)[m @ [36mSpringBatch[0;1m ---[m [INFO] Building jar: C:\Users\mahes\eclipse-workspace\SpringBatch\target\SpringBatch-0.0.1-SNAPSHOT.jar [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 4.103 s [INFO] Finished at: 2025-09-27T12:08:24+05:30 [INFO] [1m------------------------------------------------------------------------[m
To check the output of the above SpringBatch program, right click on App.java class and select run as -> Java application. It will produce the following output −
Sept 27, 2025 12:30:44 PM org.springframework.batch.core.launch.support.TaskExecutorJobLauncher afterPropertiesSet INFO: No TaskExecutor has been set, defaulting to synchronous executor. Sept 27, 2025 12:30:46 PM org.springframework.batch.core.launch.support.TaskExecutorJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}] Sept 27, 2025 12:30:46 PM org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1] Hello This is a sample example of spring batch Sept 27, 2025 12:30:46 PM org.springframework.batch.core.step.AbstractStep execute INFO: Step: [step1] executed in 70ms Sept 27, 2025 12:30:46 PM org.springframework.batch.core.launch.support.TaskExecutorJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 240ms Exit Status : COMPLETED