1. Batch Applications

This section answers questions that arise from using Spring Batch with Spring Boot.

By default, batch applications require a DataSource to store job details. Batch autowires a single DataSource in your context and uses that for processing. To have Batch use a DataSource other than the application’s main DataSource, declare a DataSource bean, annotating its @Bean method with @BatchDataSource. If you do so and want two data sources, remember to create another one and mark it as @Primary. To take greater control, implement BatchConfigurer. See The Javadoc of @EnableBatchProcessing for more details.

For more about Spring Batch, see the Spring Batch project page.

1.1. Execute Spring Batch Jobs on Startup

Spring Batch auto-configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context.

By default, it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring.batch.job.names (which takes a comma-separated list of job name patterns).

Specifying job parameters on the command line

Unlike command line option arguments that set properties in the Environment (i.e. by starting with --, such as --my-property=value), job parameters have to be specified on the command line without dashes (e.g. jobParam=value).

If the application context includes a JobRegistry, the jobs in spring.batch.job.names are looked up in the registry instead of being autowired from the context. This is a common pattern with more complex systems, where multiple jobs are defined in child contexts and registered centrally.