此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Batch 文档 5.2.2! |
批处理基础结构配置
如前所述,Spring Batch 依赖于许多基础设施 Bean 来作作业和步骤,
包括JobOperator
和JobRepository
.虽然可以手动定义这些 bean,但使用@EnableBatchProcessing
注释或DefaultBatchConfiguration
class 来提供基本配置。
默认情况下,Spring Batch 将提供无资源批处理基础设施配置,该配置基于
这ResourcelessJobRepository
实现。如果要使用数据库支持的作业存储库,可以
使用@EnableJdbcJobRepository
/ @EnableMongoJobRepository
注释或等效类JdbcDefaultBatchConfiguration
/ MongoDefaultBatchConfiguration
如配置 JobRepository 部分中所述。
基于注释的配置
这@EnableBatchProcessing
注释的工作原理与其他@Enable*
注释中的
Spring家族。在这种情况下,@EnableBatchProcessing
提供基本配置
构建批处理作业。在此基本配置中,一个StepScope
和JobScope
是
创建,此外还有许多可供自动连接的 bean:
-
JobRepository
:名为jobRepository
-
JobOperator
:名为jobOperator
-
JobRegistry
:名为jobRegistry
以下是如何使用@EnableBatchProcessing
Java 配置类中的注释:
@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {
@Bean
public Job job(JobRepository jobRepository) {
return new JobBuilder("myJob", jobRepository)
//define job flow as needed
.build();
}
}
可以使用
这@EnableBatchProcessing
注解。
只有一个配置类需要具有@EnableBatchProcessing 注解。一次
你有一个用它注释的类,你拥有前面描述的所有配置。 |
编程配置
与基于注释的配置类似,一种配置基础结构的编程方式
beans 是通过DefaultBatchConfiguration
类。此类提供相同的 bean
提供方@EnableBatchProcessing
并可用作基类来配置批处理作业。
以下代码段是如何使用它的典型示例:
@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {
@Bean
public Job job(JobRepository jobRepository) {
return new JobBuilder("myJob", jobRepository)
// define job flow as needed
.build();
}
}
您可以通过覆盖所需的 setter 来自定义任何基础架构 Bean 的配置。
@EnableBatchProcessing 不应与DefaultBatchConfiguration .你应该
要么使用声明式方式配置 Spring Batch@EnableBatchProcessing ,
或使用编程方式扩展DefaultBatchConfiguration ,但不能同时在
同时。 |