Recommend using defaultCandidate=false on qualified beans
Closes gh-42831
This commit is contained in:
@@ -536,13 +536,12 @@ class BatchAutoConfigurationTests {
|
||||
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();
|
||||
}
|
||||
@@ -564,7 +563,7 @@ class BatchAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@BatchTransactionManager
|
||||
@Bean
|
||||
@Bean(defaultCandidate = false)
|
||||
PlatformTransactionManager batchTransactionManager() {
|
||||
return mock(PlatformTransactionManager.class);
|
||||
}
|
||||
@@ -575,13 +574,12 @@ class BatchAutoConfigurationTests {
|
||||
static class BatchTaskExecutorConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
TaskExecutor taskExecutor() {
|
||||
return new SyncTaskExecutor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@BatchTaskExecutor
|
||||
@Bean(defaultCandidate = false)
|
||||
TaskExecutor batchTaskExecutor() {
|
||||
return new SimpleAsyncTaskExecutor();
|
||||
}
|
||||
|
||||
@@ -317,7 +317,8 @@ class FlywayAutoConfigurationTests {
|
||||
.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)))
|
||||
@@ -928,13 +929,12 @@ class FlywayAutoConfigurationTests {
|
||||
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();
|
||||
}
|
||||
@@ -955,7 +955,7 @@ class FlywayAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@FlywayDataSource
|
||||
@Bean
|
||||
@Bean(defaultCandidate = false)
|
||||
DataSource flywayDataSource() {
|
||||
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -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`.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user