
spring batch 5.0.0 版本引入了显著的配置简化,废弃了 `abstractbatchconfiguration` 和 `batchconfigurer` 等旧有配置类。本文旨在指导开发者如何应对这些变化,重点介绍新版本中基于spring boot自动配置和声明式job/step定义的现代化方法,并强调查阅官方升级指南的重要性,以确保平滑升级和高效开发。
Spring Batch 5.0.0 配置简化概述
Spring Batch 5.0.0 版本对配置模型进行了重大调整,旨在进一步简化批处理应用的开发和部署,尤其是在与Spring Boot结合使用时。其中一个核心变化是移除了早期版本中用于基础设施配置的某些抽象类和接口,例如 AbstractBatchConfiguration 和 BatchConfigurer。这些类的废弃意味着开发者不再需要通过继承或实现它们来配置批处理核心组件,如 JobRepository、JobLauncher、JobExplorer 和 PlatformTransactionManager。
这一改变的主要驱动力是利用Spring Boot的自动配置能力。在Spring Batch 5.0.0 及更高版本中,当检测到Spring Batch依赖时,Spring Boot会自动配置大部分批处理基础设施,极大地减少了样板代码。
废弃的配置类及其替代方案
在旧版本的Spring Batch中,开发者可能通过以下方式进行配置:
import org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
// ...
@Configuration("org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration")
public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
// 可能会重写一些方法来定制BatchConfigurer
}在Spring Batch 5.0.0 中,AbstractBatchConfiguration 和 BatchConfigurer 已不再推荐使用,并且在某些情况下可能已被移除。
替代方案与现代化方法:
-
利用Spring Boot自动配置: 在大多数情况下,如果您使用的是Spring Boot项目,并且在pom.xml中正确引入了spring-batch-core依赖,Spring Boot会自动为您配置批处理所需的基础设施。这意味着您通常不需要显式地定义JobRepository、JobLauncher、JobExplorer和PlatformTransactionManager等Bean。
POM 依赖示例:
org.springframework.batch spring-batch-core org.springframework.boot spring-boot-starter-batch org.springframework.batch spring-batch-integration org.springframework.retry spring-retry spring-boot-starter-batch 会自动引入 spring-batch-core 并提供自动配置。
-
自定义批处理基础设施Bean: 如果您需要对批处理基础设施进行高级定制(例如,使用特定的数据源、事务管理器或不同的JobRepository实现),您仍然可以通过在Spring配置类中声明相应的Bean来覆盖自动配置。Spring Boot的自动配置机制会优先使用您自定义的Bean。
例如,如果您需要使用特定的DataSource和PlatformTransactionManager来配置JobRepository,可以这样做:
import javax.sql.DataSource; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @Configuration public class CustomBatchInfrastructureConfiguration { // 假设您已经定义了一个名为 'batchDataSource' 的数据源Bean // 如果没有,Spring Boot会自动配置一个默认数据源 @Bean public PlatformTransactionManager transactionManager(DataSource batchDataSource) { return new DataSourceTransactionManager(batchDataSource); } @Bean public JobRepository jobRepository(DataSource batchDataSource, PlatformTransactionManager transactionManager) throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(batchDataSource); factory.setTransactionManager(transactionManager); factory.setIsolationLevelForCreate("ISOLATION_SERIALIZABLE"); // 示例:设置隔离级别 factory.setTablePrefix("BATCH_"); // 示例:设置表前缀 factory.afterPropertiesSet(); return factory.getObject(); } // JobLauncher、JobExplorer等也可以通过类似方式自定义 } -
声明式Job和Step定义: 在Spring Batch 5.0.0 中,定义Job和Step的核心方式是使用JobBuilderFactory和StepBuilderFactory(在Spring Batch 5.x 中已更新为JobBuilder和StepBuilder)。您只需在配置类中定义Job和Step作为Bean即可。
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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; @Configuration public class MyBatchJobConfiguration { // JobRepository 和 PlatformTransactionManager 通常由Spring Boot自动配置提供 // 如果您自定义了,这里会注入您自定义的Bean private final JobRepository jobRepository; private final PlatformTransactionManager transactionManager; public MyBatchJobConfiguration(JobRepository jobRepository, PlatformTransactionManager transactionManager) { this.jobRepository = jobRepository; this.transactionManager = transactionManager; } @Bean public Job myProcessingJob() { return new JobBuilder("myProcessingJob", jobRepository) .start(step1()) .next(step2()) .build(); } @Bean public Step step1() { return new StepBuilder("step1", jobRepository) .chunk(10, transactionManager) // 泛型类型为 ItemReader 的输入和 ItemWriter 的输出 .reader(itemReader()) .processor(itemProcessor()) .writer(itemWriter()) .build(); } @Bean public Step step2() { return new StepBuilder("step2", jobRepository) . chunk(5, transactionManager) .reader(anotherItemReader()) .processor(anotherItemProcessor()) .writer(anotherItemWriter()) .build(); } // 示例:定义 ItemReader, ItemProcessor, ItemWriter beans @Bean public ItemReader itemReader() { // 实现您的 ItemReader 逻辑 return () -> "Hello"; // 示例 } @Bean public ItemProcessor itemProcessor() { // 实现您的 ItemProcessor 逻辑 return item -> item.toUpperCase(); // 示例 } @Bean public ItemWriter itemWriter() { // 实现您的 ItemWriter 逻辑 return items -> items.forEach(System.out::println); // 示例 } @Bean public ItemReader anotherItemReader() { /* ... */ return () -> "World"; } @Bean public ItemProcessor anotherItemProcessor() { /* ... */ return item -> item + "!"; } @Bean public ItemWriter anotherItemWriter() { /* ... */ return items -> items.forEach(System.err::println); } }
升级注意事项与最佳实践
查阅官方升级指南: 这是最关键的一步。Spring Batch的每个主要版本都会发布详细的升级指南和发布说明。对于Spring Batch 5.0.0,请务必查阅官方文档的"What's New"和"Upgrade Guide"部分。这些文档会详细列出所有重大变更、废弃项以及推荐的迁移路径。官方文档通常是解决升级问题的最佳资源。
逐步升级与测试: 如果您的项目较大,建议分阶段进行升级。先升级Spring Boot版本到与Spring Batch 5.0.0 兼容的版本,然后逐步升级Spring Batch。在每个阶段进行彻底的单元测试和集成测试,以确保所有批处理作业都能正常运行。
移除旧的配置类: 识别并移除项目中对AbstractBatchConfiguration和BatchConfigurer的依赖。将相关的配置逻辑迁移到新的声明式Bean定义或依赖Spring Boot的自动配置。
关注日志输出: 在升级后运行应用程序时,密切关注控制台和日志输出。Spring框架通常会在启动时打印出警告或错误信息,提示关于废弃API的使用或配置问题。
理解Spring Boot的批处理属性: Spring Boot提供了一系列spring.batch.*配置属性,可以在application.properties或application.yml中进行配置,以微调批处理的行为,例如禁用自动配置、设置表前缀等。
总结
Spring Batch 5.0.0 的升级带来了更简洁、更现代的批处理应用开发体验。通过充分利用Spring Boot的自动配置能力,并采用声明式Job和Step的定义方式,开发者可以显著减少配置代码,提高开发效率。在升级过程中,务必以官方文档为核心指导,并结合逐步测试的策略,确保平稳过渡到新版本。










