Spring Boot
Batch Jobs
Java
Spring Framework
Application Development

How Spring Boot run batch jobs

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Boot runs batch jobs through Spring Batch infrastructure that it auto-configures when the right dependencies are present. In the common startup flow, Boot detects Job beans, creates the supporting batch components, and launches eligible jobs automatically unless you tell it not to.

The Main Pieces Spring Batch Uses

A Spring Batch application usually revolves around a few core concepts:

  • 'Job: the full batch workflow'
  • 'Step: one stage of that workflow'
  • 'JobRepository: metadata about job executions'
  • 'JobLauncher: the component that starts jobs'

Spring Boot helps by auto-configuring much of this plumbing so you can focus on defining the job itself.

A Minimal Batch Job in Spring Boot

Here is a small example with one tasklet-based step:

java
1import org.springframework.batch.core.Job;
2import org.springframework.batch.core.Step;
3import org.springframework.batch.core.job.builder.JobBuilder;
4import org.springframework.batch.core.launch.support.RunIdIncrementer;
5import org.springframework.batch.core.repository.JobRepository;
6import org.springframework.batch.core.step.builder.StepBuilder;
7import org.springframework.batch.repeat.RepeatStatus;
8import org.springframework.context.annotation.Bean;
9import org.springframework.context.annotation.Configuration;
10import org.springframework.transaction.PlatformTransactionManager;
11
12@Configuration
13public class BatchConfig {
14
15    @Bean
16    Step exampleStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
17        return new StepBuilder("exampleStep", jobRepository)
18            .tasklet((contribution, chunkContext) -> {
19                System.out.println("Running batch step");
20                return RepeatStatus.FINISHED;
21            }, transactionManager)
22            .build();
23    }
24
25    @Bean
26    Job exampleJob(JobRepository jobRepository, Step exampleStep) {
27        return new JobBuilder("exampleJob", jobRepository)
28            .incrementer(new RunIdIncrementer())
29            .start(exampleStep)
30            .build();
31    }
32}

If this application starts with batch auto-run enabled, Spring Boot can launch exampleJob automatically at startup.

Why the Job Runs on Startup

When Spring Boot sees Spring Batch on the classpath and finds batch job beans, it can use a job-launching runner during application startup. That is why developers are often surprised to see their batch job execute immediately after the application context finishes loading.

In other words, the job is not running by magic. It is being launched by startup infrastructure that Boot wires in for you.

How to Control Which Jobs Run

If you want only a specific job to run, configure it explicitly in application properties:

properties
spring.batch.job.name=exampleJob

If you do not want jobs to auto-run at startup at all:

properties
spring.batch.job.enabled=false

Then you can launch jobs yourself through JobLauncher, a scheduler, an API endpoint, or some other trigger.

Manual Job Launching

Here is a simple manual launch example:

java
1import org.springframework.batch.core.Job;
2import org.springframework.batch.core.JobParameters;
3import org.springframework.batch.core.JobParametersBuilder;
4import org.springframework.batch.core.launch.JobLauncher;
5import org.springframework.boot.CommandLineRunner;
6import org.springframework.context.annotation.Bean;
7
8@Bean
9CommandLineRunner runJob(JobLauncher jobLauncher, Job exampleJob) {
10    return args -> {
11        JobParameters params = new JobParametersBuilder()
12            .addLong("timestamp", System.currentTimeMillis())
13            .toJobParameters();
14
15        jobLauncher.run(exampleJob, params);
16    };
17}

Using a timestamp parameter avoids accidental job-instance collisions when the same job is launched repeatedly.

Chunk Steps Versus Tasklets

The example above uses a tasklet because it is compact. Real batch systems often use chunk-oriented processing for reading, transforming, and writing many records. Spring Boot does not change that model. It simply makes it easier to wire the infrastructure and external configuration around it.

So when people ask how Spring Boot runs batch jobs, the practical answer is:

  1. Spring Batch defines the execution model.
  2. Spring Boot auto-configures the infrastructure.
  3. A launcher or runner starts the job.

Common Pitfalls

The most common mistake is forgetting that jobs may auto-run on startup. That can surprise developers during local testing.

Another issue is running the same job with the same parameters and then wondering why nothing new happens. Spring Batch tracks job instances by parameters.

Teams also disable auto-run and then forget to add another trigger path, which makes it look as though the job definition is broken when it simply is not being launched.

Summary

  • Spring Boot runs batch jobs by auto-configuring Spring Batch infrastructure and launching detected jobs.
  • 'Job, Step, JobRepository, and JobLauncher are the main moving parts.'
  • Jobs can auto-run at startup unless you disable that behavior.
  • Use properties such as spring.batch.job.name and spring.batch.job.enabled to control startup execution.
  • For repeated launches, vary job parameters so Spring Batch treats each run as a new job instance.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.