Polish "Add support for configuring a Batch-specific DataSource"

See gh-17375
This commit is contained in:
Andy Wilkinson 2019-07-08 13:14:27 +01:00
parent f449665e2a
commit 072453bf58
4 changed files with 17 additions and 15 deletions

View File

@ -76,10 +76,10 @@ public class BatchAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(DataSource.class)
public BatchDataSourceInitializer batchDataSourceInitializer(ObjectProvider<DataSource> dataSource,
public BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader) {
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(dataSource::getIfAvailable),
resourceLoader, this.properties);
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
this.properties);
}
@Bean

View File

@ -36,7 +36,6 @@ import org.springframework.transaction.PlatformTransactionManager;
*/
@ConditionalOnClass(PlatformTransactionManager.class)
@ConditionalOnMissingBean(BatchConfigurer.class)
@ConditionalOnBean(DataSource.class)
@Configuration(proxyBeanMethods = false)
class BatchConfigurerConfiguration {
@ -45,10 +44,10 @@ class BatchConfigurerConfiguration {
static class JdbcBatchConfiguration {
@Bean
BasicBatchConfigurer batchConfigurer(BatchProperties properties, ObjectProvider<DataSource> dataSource,
BasicBatchConfigurer batchConfigurer(BatchProperties properties, DataSource dataSource,
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
return new BasicBatchConfigurer(properties, batchDataSource.getIfAvailable(dataSource::getIfAvailable),
return new BasicBatchConfigurer(properties, batchDataSource.getIfAvailable(() -> dataSource),
transactionManagerCustomizers.getIfAvailable());
}
@ -60,11 +59,11 @@ class BatchConfigurerConfiguration {
static class JpaBatchConfiguration {
@Bean
JpaBatchConfigurer batchConfigurer(BatchProperties properties, ObjectProvider<DataSource> dataSource,
JpaBatchConfigurer batchConfigurer(BatchProperties properties, DataSource dataSource,
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers,
EntityManagerFactory entityManagerFactory) {
return new JpaBatchConfigurer(properties, batchDataSource.getIfAvailable(dataSource::getIfAvailable),
return new JpaBatchConfigurer(properties, batchDataSource.getIfAvailable(() -> dataSource),
transactionManagerCustomizers.getIfAvailable(), entityManagerFactory);
}

View File

@ -23,9 +23,12 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
/**
* Qualifier annotation to allow a Batch-specific DataSource.
* Qualifier annotation for a DataSource to be injected into Batch auto-configuration. Can
* be used on a secondary data source, if there is another one marked as
* {@link Primary @Primary}.
*
* @author Dmytro Nosan
* @since 2.2.0

View File

@ -2430,12 +2430,12 @@ other factory that your application defines, if any.
This section answers questions that arise from using Spring Batch with Spring Boot.
NOTE: By default, batch applications require a `DataSource` to store job details.
Batch autowires a single `DataSource` in your context and
uses that for processing. If you like to use a different `DataSource`, you can create
one and mark it is `@Bean` as `@BatchDataSource`. If you do so and want two data sources,
remember to create another one and mark it as `@Primary`. If you want to deviate from that,
you need to implement `BatchConfigurer`. See
{spring-batch-javadoc}/core/configuration/annotation/EnableBatchProcessing.html[The
Batch autowires a single `DataSource` in your context and uses that for processing. To
have Batch use a `DataSource` other than the applications 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
{spring-batch-javadoc}/core/configuration/annotation/EnableBatchProcessing.html[the
Javadoc of `@EnableBatchProcessing`] for more details.
For more about Spring Batch, see the https://projects.spring.io/spring-batch/[Spring Batch