Skip to content

Commit

Permalink
Recommend using defaultCandidate=false on qualified beans
Browse files Browse the repository at this point in the history
Closes gh-42831
  • Loading branch information
wilkinsona committed Oct 22, 2024
1 parent 32af304 commit e26c6d6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,12 @@ private Job mockJob(String name) {
static class BatchDataSourceConfiguration {

@Bean
@Primary
DataSource normalDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
}

@BatchDataSource
@Bean
@Bean(defaultCandidate = false)
DataSource batchDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:batchdatasource").username("sa").build();
}
Expand All @@ -564,7 +563,7 @@ PlatformTransactionManager normalTransactionManager() {
}

@BatchTransactionManager
@Bean
@Bean(defaultCandidate = false)
PlatformTransactionManager batchTransactionManager() {
return mock(PlatformTransactionManager.class);
}
Expand All @@ -575,13 +574,12 @@ PlatformTransactionManager batchTransactionManager() {
static class BatchTaskExecutorConfiguration {

@Bean
@Primary
TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}

@Bean
@BatchTaskExecutor
@Bean(defaultCandidate = false)
TaskExecutor batchTaskExecutor() {
return new SimpleAsyncTaskExecutor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ void schemaManagementProviderDetectsDataSource() {
.run((context) -> {
FlywaySchemaManagementProvider schemaManagementProvider = context
.getBean(FlywaySchemaManagementProvider.class);
assertThat(schemaManagementProvider.getSchemaManagement(context.getBean(DataSource.class)))
assertThat(schemaManagementProvider
.getSchemaManagement(context.getBean("normalDataSource", DataSource.class)))
.isEqualTo(SchemaManagement.UNMANAGED);
assertThat(schemaManagementProvider
.getSchemaManagement(context.getBean("flywayDataSource", DataSource.class)))
Expand Down Expand Up @@ -928,13 +929,12 @@ private ContextConsumer<AssertableApplicationContext> validateFlywayTeamsPropert
static class FlywayDataSourceConfiguration {

@Bean
@Primary
DataSource normalDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
}

@FlywayDataSource
@Bean
@Bean(defaultCandidate = false)
DataSource flywayDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build();
}
Expand All @@ -955,7 +955,7 @@ DataSource secondDataSource() {
}

@FlywayDataSource
@Bean
@Bean(defaultCandidate = false)
DataSource flywayDataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This section addresses those questions.
By default, batch applications require a `DataSource` to store job details.
Spring Batch expects a single `DataSource` by default.
To have it 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 mark the other one `@Primary`.
If you do so and want two data sources (for example by retaining the main auto-configured `DataSource`), set the `defaultCandidate` attribute of the `@Bean` annotation to `false`.
To take greater control, add `@EnableBatchProcessing` to one of your `@Configuration` classes or extend `DefaultBatchConfiguration`.
See the API documentation of javadoc:{url-spring-batch-javadoc}/org.springframework.batch.core.configuration.annotation.EnableBatchProcessing[format=annotation]
and javadoc:{url-spring-batch-javadoc}/org.springframework.batch.core.configuration.support.DefaultBatchConfiguration[] for more details.
Expand All @@ -24,16 +24,16 @@ For more info about Spring Batch, see the {url-spring-batch-site}[Spring Batch p
[[howto.batch.specifying-a-transaction-manager]]
== Specifying a Batch Transaction Manager

Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a `PlatformTransactionManager` for use in the batch processing by marking it as `@BatchTransactionManager`.
If you do so and want two transaction managers, remember to mark the other one as `@Primary`.
Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a `PlatformTransactionManager` for use in batch processing by annotating its `@Bean` method with `@BatchTransactionManager`.
If you do so and want two transaction managers (for example by retaining the auto-configured `PlatformTransactionManager`), set the `defaultCandidate` attribute of the `@Bean` annotation to `false`.



[[howto.batch.specifying-a-task-executor]]
== Specifying a Batch Task Executor

Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a `TaskExecutor` for use in the batch processing by marking it as `@BatchTaskExecutor`.
If you do so and want two task executors, remember to mark the other one as `@Primary`.
Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[], you can define a `TaskExecutor` for use in batch processing by annotating its `@Bean` method with `@BatchTaskExecutor`.
If you do so and want two task executors (for example by retaining the auto-configured `TaskExecutor`), set the `defaultCandidate` attribute of the `@Bean` annotation to `false`.



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Beans that implement the deprecated `FlywayCallback` interface can also be detec

By default, Flyway autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
If you like to use a different `DataSource`, you can create one and mark its `@Bean` as `@FlywayDataSource`.
If you do so and want two data sources, remember to create another one and mark it as `@Primary`.
If you do so and want two data sources (for example by retaining the main auto-configured `DataSource`), remember to set the `defaultCandidate` attribute of the `@Bean` annotation to `false`.
Alternatively, you can use Flyway's native `DataSource` by setting `spring.flyway.[url,user,password]` in external properties.
Setting either `spring.flyway.url` or `spring.flyway.user` is sufficient to cause Flyway to use its own `DataSource`.
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
Expand Down Expand Up @@ -184,7 +184,7 @@ In addition to YAML, Liquibase also supports JSON, XML, and SQL change log forma

By default, Liquibase autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
If you need to use a different `DataSource`, you can create one and mark its `@Bean` as `@LiquibaseDataSource`.
If you do so and you want two data sources, remember to create another one and mark it as `@Primary`.
If you do so and want two data sources (for example by retaining the main auto-configured `DataSource`), remember to set the `defaultCandidate` attribute of the `@Bean` annotation to `false`.
Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[driver-class-name,url,user,password]` in external properties.
Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`.
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
Expand Down

0 comments on commit e26c6d6

Please sign in to comment.