diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 1121e8fc8453..8f10bea4e035 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -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(); } @@ -564,7 +563,7 @@ PlatformTransactionManager normalTransactionManager() { } @BatchTransactionManager - @Bean + @Bean(defaultCandidate = false) PlatformTransactionManager batchTransactionManager() { return mock(PlatformTransactionManager.class); } @@ -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(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java index 8522befc2596..49178e27473c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java @@ -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))) @@ -928,13 +929,12 @@ private ContextConsumer 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(); } @@ -955,7 +955,7 @@ DataSource secondDataSource() { } @FlywayDataSource - @Bean + @Bean(defaultCandidate = false) DataSource flywayDataSource() { return DataSourceBuilder.create().url("jdbc:hsqldb:mem:flywaytest").username("sa").build(); } diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/batch.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/batch.adoc index a682256327dc..7da89c464644 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/batch.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/batch.adoc @@ -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`. diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc index 10ce16925e7f..66c28e21dee2 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc @@ -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.