Spring Batch 作业和步骤
将批处理组织成多个步骤
Spring Batch 作业和步骤 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Spring Batch?
Spring Batch is a framework for robust, large-volume batch processing: reading, transforming and writing data in bulk with built-in transaction management, chunking, and restartability.
- ETL jobs, migrations, nightly reports
- Handles millions of records reliably
Adding the dependency
Add spring-boot-starter-batch. Spring Batch needs a datasource to store its metadata (job and step execution history).
<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>Core concepts: Job and Step
A Job is the whole batch process. It is made of one or more Steps executed in sequence. Each Step does one phase of work.
Job= the overall unitStep= a phase (read-process-write or a tasklet)
JobRepository and metadata
The JobRepository persists execution state to database tables (BATCH_JOB_EXECUTION, BATCH_STEP_EXECUTION...). This is what enables restartability and prevents re-running a completed job instance.
Defining a Job with JobBuilder
Use JobBuilder to assemble a job. You give it a name, a JobRepository, and a starting step.
@Bean
public Job importJob(JobRepository repo, Step step1) {
return new JobBuilder("importJob", repo)
.start(step1)
.build();
}Defining a Step with StepBuilder
StepBuilder builds a step. A chunk-oriented step declares the input/output types, a chunk size, and the reader, processor and writer.
@Bean
public Step step1(JobRepository repo,
PlatformTransactionManager tx,
ItemReader<String> reader,
ItemWriter<String> writer) {
return new StepBuilder("step1", repo)
.<String, String>chunk(10, tx)
.reader(reader)
.writer(writer)
.build();
}Chunk-oriented processing
Chunk processing reads items one by one, accumulates them up to the chunk size, then writes the whole chunk in a single transaction. If the chunk fails it rolls back together.
- read 10 items -> process each -> write 10 -> commit
Tasklet steps
For a single unit of work (delete a file, run a stored procedure) use a Tasklet step instead of reader/writer.
@Bean
public Step cleanupStep(JobRepository repo, PlatformTransactionManager tx) {
return new StepBuilder("cleanup", repo)
.tasklet((contribution, context) -> {
deleteTempFiles();
return RepeatStatus.FINISHED;
}, tx)
.build();
}Chaining multiple steps
Run steps in order with .next(). The job moves to the next step only if the previous one succeeds.
@Bean
public Job pipeline(JobRepository repo, Step extract, Step transform, Step load) {
return new JobBuilder("etl", repo)
.start(extract)
.next(transform)
.next(load)
.build();
}JobParameters
A JobInstance is identified by its name plus JobParameters. Different parameters (e.g. a date) create distinct instances, which is how you re-run a job for a new day.
JobParameters params = new JobParametersBuilder()
.addString("runDate", "2026-05-30")
.toJobParameters();
jobLauncher.run(importJob, params);Launching jobs in Spring Boot
By default Spring Boot runs all defined jobs on startup. Disable that with spring.batch.job.enabled=false and launch manually via JobLauncher when you need control (e.g. on a schedule).
Quick Check
Check your grasp of Job and Step structure.
Recap
You now understand the batch model:
Jobcontains orderedStepsJobBuilder/StepBuilderassemble them- Chunk steps read-process-write in transactions; tasklet steps do single units
JobParametersidentify aJobInstanceand enable re-runs
常见问题解答
「Spring Batch 作业和步骤」课时是免费的吗?
是的 — 「Spring Batch 作业和步骤」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「Spring Batch 作业和步骤」这节课中我会学到什么?
将批处理组织成多个步骤 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「Spring Batch 作业和步骤」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?
能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 @Scheduled 进行调度
- Spring Batch 作业和步骤
- 读取器、处理器和写入器
- 重启和错误处理