Spring Batch启动报错:无法找到JobBuilderFactory Bean,求解决方案
问题分析与解决方案
核心原因
你遇到的JobBuilderFactory bean找不到问题,主要分两种情况:
- Spring Batch 5.x版本:
JobBuilderFactory和StepBuilderFactory已被完全移除,官方不再提供这两个类,这是最常见的原因。 - Spring Batch 4.x版本:
@EnableBatchProcessing注解会自动注册这两个工厂的bean,但可能存在配置类未被扫描或依赖缺失的问题。
解决方案
情况1:使用Spring Batch 5.x(推荐)
直接使用官方推荐的新API,通过JobBuilder和StepBuilder的静态方法构建Job和Step,无需再注入旧的工厂类。
修改你的JobConfig代码如下:
package com.batch.springBatch.config; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.job.builder.JobBuilder; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import com.batch.springBatch.entity.Employee; @Configuration public class JobConfig { @Value("${app.batch.step.name}") private String batchStepName; @Value("${app.batch.chunk.size}") private int chunkSize; @Value("${app.batch.job.name}") private String jobName; @Autowired private JobRepository jobRepository; @Autowired private PlatformTransactionManager transactionManager; @Autowired private ItemReader<Employee> reader; @Autowired private ItemProcessor<Employee, Employee> processor; @Autowired ItemWriter<Employee> writer; @Bean public Step step1() { return new StepBuilder(batchStepName, jobRepository) .<Employee, Employee>chunk(chunkSize, transactionManager) .reader(reader) .processor(processor) .writer(writer) .build(); } @Bean public Job runJob() { return new JobBuilder(jobName, jobRepository) .flow(step1()) .end() .build(); } }
- 注意:
BatchConfig中的@EnableBatchProcessing注解可以保留,它会自动注册JobRepository和PlatformTransactionManager等核心bean(前提是你已经正确配置了MySQL数据源)。
情况2:使用Spring Batch 4.x
如果坚持使用旧版本,按以下步骤排查:
- 确保
@EnableBatchProcessing所在的BatchConfig类被Spring上下文扫描到(主启动类的@SpringBootApplication默认会扫描同包及子包,你的结构没问题)。 - 检查依赖是否正确引入:
Maven依赖:
Gradle依赖:<dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> </dependency>implementation 'org.springframework.batch:spring-batch-core' - 无需额外添加
@ComponentScan,只要配置类在扫描范围内即可。
内容的提问来源于stack exchange,提问作者Akash Kumar
相关产品推荐
相关产品推荐

