此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Batch 文档 5.2.2spring-doc.cadn.net.cn

批处理基础结构配置

如前所述,Spring Batch 依赖于许多基础设施 Bean 来作作业和步骤, 包括JobOperatorJobRepository.虽然可以手动定义这些 bean,但使用@EnableBatchProcessing注释或DefaultBatchConfigurationclass 来提供基本配置。spring-doc.cadn.net.cn

默认情况下,Spring Batch 将提供无资源批处理基础设施配置,该配置基于 这ResourcelessJobRepository实现。如果要使用数据库支持的作业存储库,可以 使用@EnableJdbcJobRepository / @EnableMongoJobRepository注释或等效类JdbcDefaultBatchConfiguration / MongoDefaultBatchConfiguration配置 JobRepository 部分中所述。spring-doc.cadn.net.cn

基于注释的配置

@EnableBatchProcessing注释的工作原理与其他@Enable*注释中的 Spring家族。在这种情况下,@EnableBatchProcessing提供基本配置 构建批处理作业。在此基本配置中,一个StepScopeJobScope是 创建,此外还有许多可供自动连接的 bean:spring-doc.cadn.net.cn

以下是如何使用@EnableBatchProcessingJava 配置类中的注释:spring-doc.cadn.net.cn

@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("myJob", jobRepository)
				//define job flow as needed
				.build();
	}

}

可以使用 这@EnableBatchProcessing注解。spring-doc.cadn.net.cn

只有一个配置类需要具有@EnableBatchProcessing注解。一次 你有一个用它注释的类,你拥有前面描述的所有配置。

编程配置

与基于注释的配置类似,一种配置基础结构的编程方式 beans 是通过DefaultBatchConfiguration类。此类提供相同的 bean 提供方@EnableBatchProcessing并可用作基类来配置批处理作业。 以下代码段是如何使用它的典型示例:spring-doc.cadn.net.cn

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("myJob", jobRepository)
				// define job flow as needed
				.build();
	}

}

您可以通过覆盖所需的 setter 来自定义任何基础架构 Bean 的配置。spring-doc.cadn.net.cn

@EnableBatchProcessing不应DefaultBatchConfiguration.你应该 要么使用声明式方式配置 Spring Batch@EnableBatchProcessing, 或使用编程方式扩展DefaultBatchConfiguration,但不能同时在 同时。