Java Configuration

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.

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

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);
	}

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

}
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.

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();
	}

}

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 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.