Stop deferring JPA bootstrap mode by default

This commit changes the default value of bootstrap-mode to "default"
rather than "deferred" so that the JPA infrastructure starts in the
main thread rather than asynchronously.

Closes gh-24249
This commit is contained in:
Stephane Nicoll 2020-12-08 15:42:14 +01:00
parent f84cb1b765
commit 06671aa50e
7 changed files with 12 additions and 12 deletions

View File

@ -96,13 +96,12 @@ public class JpaRepositoriesAutoConfiguration {
} }
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "bootstrap-mode", @ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "bootstrap-mode",
havingValue = "deferred", matchIfMissing = true) havingValue = "deferred")
static class DeferredBootstrapMode { static class DeferredBootstrapMode {
} }
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "bootstrap-mode", havingValue = "lazy", @ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "bootstrap-mode", havingValue = "lazy")
matchIfMissing = false)
static class LazyBootstrapMode { static class LazyBootstrapMode {
} }

View File

@ -57,7 +57,7 @@ class JpaRepositoriesRegistrar extends AbstractRepositoryConfigurationSourceSupp
@Override @Override
protected BootstrapMode getBootstrapMode() { protected BootstrapMode getBootstrapMode() {
return (this.bootstrapMode == null) ? BootstrapMode.DEFERRED : this.bootstrapMode; return (this.bootstrapMode == null) ? BootstrapMode.DEFAULT : this.bootstrapMode;
} }
@Override @Override

View File

@ -622,7 +622,7 @@
"name": "spring.data.jpa.repositories.bootstrap-mode", "name": "spring.data.jpa.repositories.bootstrap-mode",
"type": "org.springframework.data.repository.config.BootstrapMode", "type": "org.springframework.data.repository.config.BootstrapMode",
"description": "Bootstrap mode for JPA repositories.", "description": "Bootstrap mode for JPA repositories.",
"defaultValue": "deferred" "defaultValue": "default"
}, },
{ {
"name": "spring.data.jpa.repositories.enabled", "name": "spring.data.jpa.repositories.enabled",

View File

@ -126,13 +126,12 @@ class JpaRepositoriesAutoConfigurationTests {
} }
@Test @Test
void bootstrapModeIsDeferredByDefault() { void bootstrapModeIsDefaultByDefault() {
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class) this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
TaskSchedulingAutoConfiguration.class)) TaskSchedulingAutoConfiguration.class))
.run((context) -> assertThat( .run((context) -> assertThat(
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()) context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull());
.isEqualTo(context.getBean("applicationTaskExecutor")));
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

View File

@ -3839,6 +3839,8 @@ To enable deferred or lazy bootstrapping, set the configprop:spring.data.jpa.rep
When using deferred or lazy bootstrapping, the auto-configured `EntityManagerFactoryBuilder` will use the context's `AsyncTaskExecutor`, if any, as the bootstrap executor. When using deferred or lazy bootstrapping, the auto-configured `EntityManagerFactoryBuilder` will use the context's `AsyncTaskExecutor`, if any, as the bootstrap executor.
If more than one exists, the one named `applicationTaskExecutor` will be used. If more than one exists, the one named `applicationTaskExecutor` will be used.
NOTE: When using deferred or lazy bootstraping, make sure to defer any access to the JPA infrastructure after the application context bootstrap phase.
TIP: We have barely scratched the surface of Spring Data JPA. TIP: We have barely scratched the surface of Spring Data JPA.
For complete details, see the {spring-data-jdbc-docs}[Spring Data JPA reference documentation]. For complete details, see the {spring-data-jdbc-docs}[Spring Data JPA reference documentation].

View File

@ -103,11 +103,11 @@ public @interface DataJpaTest {
/** /**
* The {@link BootstrapMode} for the test repository support. Defaults to * The {@link BootstrapMode} for the test repository support. Defaults to
* {@link BootstrapMode#LAZY}. * {@link BootstrapMode#DEFAULT}.
* @return the {@link BootstrapMode} to use for testing the repository * @return the {@link BootstrapMode} to use for testing the repository
*/ */
@PropertyMapping("spring.data.jpa.repositories.bootstrap-mode") @PropertyMapping("spring.data.jpa.repositories.bootstrap-mode")
BootstrapMode bootstrapMode() default BootstrapMode.LAZY; BootstrapMode bootstrapMode() default BootstrapMode.DEFAULT;
/** /**
* Determines if default filtering should be used with * Determines if default filtering should be used with

View File

@ -109,9 +109,9 @@ class DataJpaTestIntegrationTests {
} }
@Test @Test
void bootstrapModeIsLazyByDefault() { void bootstrapModeIsDefaultByDefault() {
assertThat(this.applicationContext.getEnvironment().getProperty("spring.data.jpa.repositories.bootstrap-mode")) assertThat(this.applicationContext.getEnvironment().getProperty("spring.data.jpa.repositories.bootstrap-mode"))
.isEqualTo(BootstrapMode.LAZY.name()); .isEqualTo(BootstrapMode.DEFAULT.name());
} }
} }