Java Configuration

Spring 3 提供了使用 Java 而不是 XML 配置应用程序的功能。从 Spring Batch 2.2.0 开始,您可以使用相同的 Java 配置来配置批处理作业。基于 Java 的配置有三个组件:@EnableBatchProcessing 注释和两个生成器。

Spring 3 brought the ability to configure applications with Java instead of XML. As of Spring Batch 2.2.0, you can configure batch jobs by using the same Java configuration. There are three components for the Java-based configuration: the @EnableBatchProcessing annotation and two builders.

@EnableBatchProcessing 注释类似于 Spring 系列中的其他 @Enable* 注释。在此情况下,@EnableBatchProcessing 为批处理作业的构建提供了一个基本配置。在此基本配置中,除了使许多 bean 可用于自动注入外,还创建了 StepScopeJobScope 的实例:

The @EnableBatchProcessing annotation works similarly to the other @Enable* annotations in the Spring family. In this case, @EnableBatchProcessing provides a base configuration for building batch jobs. Within this base configuration, an instance of StepScope and JobScope are created, in addition to a number of beans being made available to be autowired:

  • JobRepository: a bean named jobRepository

  • JobLauncher: a bean named jobLauncher

  • JobRegistry: a bean named jobRegistry

  • JobExplorer: a bean named jobExplorer

  • JobOperator: a bean named jobOperator

默认实现提供前面列出的 bean,并且要求将 DataSourcePlatformTransactionManager 提供为上下文中内的 bean。JobRepositoryJobExplorer 实例使用数据源和事务管理器。默认情况下,将使用名为 dataSource 的数据源和名为 transactionManager 的事务管理器。您可以使用 @EnableBatchProcessing 注释的属性来自定义其中任何一个 bean。以下示例展示了如何提供自定义数据源和事务管理器:

The default implementation provides the beans mentioned in the preceding list and requires a DataSource and a PlatformTransactionManager to be provided as beans within the context. The data source and transaction manager are used by the JobRepository and JobExplorer instances. By default, the data source named dataSource and the transaction manager named transactionManager will be used. You can customize any of these beans by using the attributes of the @EnableBatchProcessing annotation. The following example shows how to provide a custom data source and transaction manager:

@Configuration
@EnableBatchProcessing(dataSourceRef = "batchDataSource", transactionManagerRef = "batchTransactionManager")
public class MyJobConfiguration {

	@Bean
	public DataSource batchDataSource() {
		return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
				.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
				.generateUniqueName(true).build();
	}

	@Bean
	public JdbcTransactionManager batchTransactionManager(DataSource dataSource) {
		return new JdbcTransactionManager(dataSource);
	}

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

}

只有一个配置类需要有 @EnableBatchProcessing 注解。一旦有了带该注解的类,你将拥有前面描述的所有配置。

Only one configuration class needs to have the @EnableBatchProcessing annotation. Once you have a class annotated with it, you have all of the configuration described earlier.

从 v5.0 开始,提供了通过 DefaultBatchConfiguration 类以编程方式配置基本基础设施 bean 的替代方式。此类提供了 @EnableBatchProcessing 提供的相同 bean,并且可以用作配置批处理作业的基本类。以下摘录是如何使用它的一个常见示例:

Starting from v5.0, an alternative, programmatic way of configuring base infrastrucutre beans is provided through the DefaultBatchConfiguration class. This class provides the same beans provided by @EnableBatchProcessing and can be used as a base class to configure batch jobs. The following snippet is a typical example of how to use it:

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

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

}

数据源和事务管理器将从应用程序上下文中解析,并在作业存储库和作业资源管理器上设置。您可以通过覆盖所需的设置器来自定义任何基础设施 bean 的配置。以下示例展示了如何自定义实例的字符编码:

The data source and transaction manager will be resolved from the application context and set on the job repository and job explorer. You can customize the configuration of any infrastructure bean by overriding the required setter. The following example shows how to customize the character encoding for instance:

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

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

	@Override
	protected Charset getCharset() {
		return StandardCharsets.ISO_8859_1;
	}
}

@EnableBatchProcessing 应该 notDefaultBatchConfiguration 一起使用。你可以使用声明式方法通过 @EnableBatchProcessing 配置 Spring Batch,或者使用通过扩展 DefaultBatchConfiguration 的编程方法,但不能同时使用两种方法。

@EnableBatchProcessing should not be used with DefaultBatchConfiguration. You should either use the declarative way of configuring Spring Batch through @EnableBatchProcessing, or use the programmatic way of extending DefaultBatchConfiguration, but not both ways at the same time.