批处理基础设施配置

如前所述,春批依赖多个基础设施豆子来作作业和步骤, 包括作业操作员以及JobRepository.虽然可以手动定义这些豆子,但使用更简单的方法。@EnableBatchProcessing注释或DefaultBatchConfiguration类以提供基础配置。spring-doc.cadn.net.cn

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

基于注释的配置

@EnableBatchProcessing注释的工作原理与其他方式类似@Enable*注释 Spring的家庭。在这种情况下,@EnableBatchProcessing提供了 的基础配置 批量作业的构建。在该基配置中,有一个StepScope工作范围是 此外,还推出了若干豆子可供自动接线: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注解。一次 你有一个类被注释了,你拥有之前描述的所有配置。

程序化配置

类似于基于注释的配置,这是一种程序化的基础设施配置方式 豆子通过DefaultBatchConfiguration类。本类提供相同的Beans 提供@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();
	}

}

你可以通过覆盖所需的设置器来自定义任何基础设施的配置。spring-doc.cadn.net.cn

@EnableBatchProcessing不应DefaultBatchConfiguration.你应该去 要么使用声明式方式配置 Spring Batch@EnableBatchProcessing, 或者使用程序化扩展方式DefaultBatchConfiguration,但并非双向 同时。