Change default BootstrapMode for JPA repositories

Change the default `BootstrapMode` for auto-configured `JpaRepositories`
to `BootstrapMode.DEFERRED` to allow the initialization of
`EntityManagerFactory` to be parallelized for increased startup efficiency.

Prior to this change, the default BootstrapMode for all auto-configured
Spring Data repositories was `BootstrapMode.DEFAULT`.

Closes gh-16230
This commit is contained in:
Scott Frederick
2020-01-21 18:06:27 -06:00
committed by Phillip Webb
parent ae3bdc79f3
commit 288889685d
6 changed files with 54 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,17 +20,18 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.repository.config.BootstrapMode;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for the {@link DataJpaTest#properties properties} attribute of
* {@link DataJpaTest @DataJpaTest}.
* Tests for non-default attributes of {@link DataJpaTest @DataJpaTest}.
*
* @author Artsiom Yudovin
* @author Scott Frederick
*/
@DataJpaTest(properties = "spring.profiles.active=test")
class DataJpaTestPropertiesIntegrationTests {
@DataJpaTest(properties = "spring.profiles.active=test", bootstrapMode = BootstrapMode.DEFERRED)
class DataJpaTestAttributesIntegrationTests {
@Autowired
private Environment environment;
@@ -40,4 +41,10 @@ class DataJpaTestPropertiesIntegrationTests {
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
}
@Test
void bootstrapModeIsSet() {
assertThat(this.environment.getProperty("spring.data.jpa.repositories.bootstrap-mode"))
.isEqualTo(BootstrapMode.DEFERRED.name());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.data.repository.config.BootstrapMode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
@@ -37,6 +38,7 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Scott Frederick
*/
@DataJpaTest
@TestPropertySource(properties = "spring.jpa.hibernate.use-new-id-generator-mappings=false")
@@ -106,4 +108,10 @@ class DataJpaTestIntegrationTests {
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
}
@Test
void bootstrapModeIsLazyByDefault() {
assertThat(this.applicationContext.getEnvironment().getProperty("spring.data.jpa.repositories.bootstrap-mode"))
.isEqualTo(BootstrapMode.LAZY.name());
}
}