Skip to content

Commit e26c6d6

Browse files
committed
Recommend using defaultCandidate=false on qualified beans
Closes gh-42831
1 parent 32af304 commit e26c6d6

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,12 @@ private Job mockJob(String name) {
536536
static class BatchDataSourceConfiguration {
537537

538538
@Bean
539-
@Primary
540539
DataSource normalDataSource() {
541540
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
542541
}
543542

544543
@BatchDataSource
545-
@Bean
544+
@Bean(defaultCandidate = false)
546545
DataSource batchDataSource() {
547546
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:batchdatasource").username("sa").build();
548547
}
@@ -564,7 +563,7 @@ PlatformTransactionManager normalTransactionManager() {
564563
}
565564

566565
@BatchTransactionManager
567-
@Bean
566+
@Bean(defaultCandidate = false)
568567
PlatformTransactionManager batchTransactionManager() {
569568
return mock(PlatformTransactionManager.class);
570569
}
@@ -575,13 +574,12 @@ PlatformTransactionManager batchTransactionManager() {
575574
static class BatchTaskExecutorConfiguration {
576575

577576
@Bean
578-
@Primary
579577
TaskExecutor taskExecutor() {
580578
return new SyncTaskExecutor();
581579
}
582580

583-
@Bean
584581
@BatchTaskExecutor
582+
@Bean(defaultCandidate = false)
585583
TaskExecutor batchTaskExecutor() {
586584
return new SimpleAsyncTaskExecutor();
587585
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ void schemaManagementProviderDetectsDataSource() {
317317
.run((context) -> {
318318
FlywaySchemaManagementProvider schemaManagementProvider = context
319319
.getBean(FlywaySchemaManagementProvider.class);
320-
assertThat(schemaManagementProvider.getSchemaManagement(context.getBean(DataSource.class)))
320+
assertThat(schemaManagementProvider
321+
.getSchemaManagement(context.getBean("normalDataSource", DataSource.class)))
321322
.isEqualTo(SchemaManagement.UNMANAGED);
322323
assertThat(schemaManagementProvider
323324
.getSchemaManagement(context.getBean("flywayDataSource", DataSource.class)))
@@ -928,13 +929,12 @@ private ContextConsumer<AssertableApplicationContext> validateFlywayTeamsPropert
928929
static class FlywayDataSourceConfiguration {
929930

930931
@Bean
931-
@Primary
932932
DataSource normalDataSource() {
933933
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
934934
}
935935

936936
@FlywayDataSource
937-
@Bean
937+
@Bean(defaultCandidate = false)
938938
DataSource flywayDataSource() {
939939
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build();
940940
}
@@ -955,7 +955,7 @@ DataSource secondDataSource() {
955955
}
956956

957957
@FlywayDataSource
958-
@Bean
958+
@Bean(defaultCandidate = false)
959959
DataSource flywayDataSource() {
960960
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build();
961961
}

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/batch.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This section addresses those questions.
1212
By default, batch applications require a `DataSource` to store job details.
1313
Spring Batch expects a single `DataSource` by default.
1414
To have it use a `DataSource` other than the application’s main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@BatchDataSource`.
15-
If you do so and want two data sources, remember to mark the other one `@Primary`.
15+
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`.
1616
To take greater control, add `@EnableBatchProcessing` to one of your `@Configuration` classes or extend `DefaultBatchConfiguration`.
1717
See the API documentation of javadoc:{url-spring-batch-javadoc}/org.springframework.batch.core.configuration.annotation.EnableBatchProcessing[format=annotation]
1818
and javadoc:{url-spring-batch-javadoc}/org.springframework.batch.core.configuration.support.DefaultBatchConfiguration[] for more details.
@@ -24,16 +24,16 @@ For more info about Spring Batch, see the {url-spring-batch-site}[Spring Batch p
2424
[[howto.batch.specifying-a-transaction-manager]]
2525
== Specifying a Batch Transaction Manager
2626

27-
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`.
28-
If you do so and want two transaction managers, remember to mark the other one as `@Primary`.
27+
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`.
28+
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`.
2929

3030

3131

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

35-
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`.
36-
If you do so and want two task executors, remember to mark the other one as `@Primary`.
35+
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`.
36+
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`.
3737

3838

3939

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Beans that implement the deprecated `FlywayCallback` interface can also be detec
146146

147147
By default, Flyway autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
148148
If you like to use a different `DataSource`, you can create one and mark its `@Bean` as `@FlywayDataSource`.
149-
If you do so and want two data sources, remember to create another one and mark it as `@Primary`.
149+
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`.
150150
Alternatively, you can use Flyway's native `DataSource` by setting `spring.flyway.[url,user,password]` in external properties.
151151
Setting either `spring.flyway.url` or `spring.flyway.user` is sufficient to cause Flyway to use its own `DataSource`.
152152
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
@@ -184,7 +184,7 @@ In addition to YAML, Liquibase also supports JSON, XML, and SQL change log forma
184184

185185
By default, Liquibase autowires the (`@Primary`) `DataSource` in your context and uses that for migrations.
186186
If you need to use a different `DataSource`, you can create one and mark its `@Bean` as `@LiquibaseDataSource`.
187-
If you do so and you want two data sources, remember to create another one and mark it as `@Primary`.
187+
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`.
188188
Alternatively, you can use Liquibase's native `DataSource` by setting `spring.liquibase.[driver-class-name,url,user,password]` in external properties.
189189
Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`.
190190
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.

0 commit comments

Comments
 (0)