Skip to content

Commit d98af26

Browse files
Change default BootstrapMode for auto-configured JPA repositories
Prior to this change, the default BootstrapMode for all auto-configured Spring Data repositories was BootstrapMode.DEFAULT. This commit changes the default BootstrapMode for auto-configured JpaRepositories to BootstrapMode.DEFERRED to allow the initialization of EntityManagerFactory to be parallelized for increased startup efficiency. The default is BootstrapMode.LAZY for tests using @DataJpaTest. Closes gh-16230
1 parent ae3bdc7 commit d98af26

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@
5656
*
5757
* @author Phillip Webb
5858
* @author Josh Long
59+
* @author Scott Frederick
5960
* @since 1.0.0
6061
* @see EnableJpaRepositories
6162
*/
@@ -95,7 +96,7 @@ private static final class BootstrapExecutorCondition extends AnyNestedCondition
9596
}
9697

9798
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "bootstrap-mode",
98-
havingValue = "deferred", matchIfMissing = false)
99+
havingValue = "deferred", matchIfMissing = true)
99100
static class DeferredBootstrapMode {
100101

101102
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesRegistrar.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Phillip Webb
3636
* @author Dave Syer
37+
* @author Scott Frederick
3738
*/
3839
class JpaRepositoriesRegistrar extends AbstractRepositoryConfigurationSourceSupport {
3940

@@ -56,7 +57,7 @@ protected RepositoryConfigurationExtension getRepositoryConfigurationExtension()
5657

5758
@Override
5859
protected BootstrapMode getBootstrapMode() {
59-
return (this.bootstrapMode == null) ? super.getBootstrapMode() : this.bootstrapMode;
60+
return (this.bootstrapMode == null) ? BootstrapMode.DEFERRED : this.bootstrapMode;
6061
}
6162

6263
@Override

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfigurationTests.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
5151
*
5252
* @author Dave Syer
5353
* @author Oliver Gierke
54+
* @author Scott Frederick
5455
*/
5556
class JpaRepositoriesAutoConfigurationTests {
5657

@@ -85,7 +86,7 @@ void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositori
8586
}
8687

8788
@Test
88-
void whenBootstrappingModeIsLazyWithMultipleAsyncExecutorBootstrapExecutorIsConfigured() {
89+
void whenBootstrapModeIsLazyWithMultipleAsyncExecutorBootstrapExecutorIsConfigured() {
8990
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
9091
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
9192
TaskSchedulingAutoConfiguration.class))
@@ -96,7 +97,7 @@ void whenBootstrappingModeIsLazyWithMultipleAsyncExecutorBootstrapExecutorIsConf
9697
}
9798

9899
@Test
99-
void whenBootstrappingModeIsLazyWithSingleAsyncExecutorBootstrapExecutorIsConfigured() {
100+
void whenBootstrapModeIsLazyWithSingleAsyncExecutorBootstrapExecutorIsConfigured() {
100101
this.contextRunner.withUserConfiguration(SingleAsyncTaskExecutorConfiguration.class)
101102
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy")
102103
.run((context) -> assertThat(
@@ -105,7 +106,7 @@ void whenBootstrappingModeIsLazyWithSingleAsyncExecutorBootstrapExecutorIsConfig
105106
}
106107

107108
@Test
108-
void whenBootstrappingModeIsDeferredBootstrapExecutorIsConfigured() {
109+
void whenBootstrapModeIsDeferredBootstrapExecutorIsConfigured() {
109110
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
110111
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
111112
TaskSchedulingAutoConfiguration.class))
@@ -116,14 +117,24 @@ void whenBootstrappingModeIsDeferredBootstrapExecutorIsConfigured() {
116117
}
117118

118119
@Test
119-
void whenBootstrappingModeIsDefaultBootstrapExecutorIsNotConfigured() {
120+
void whenBootstrapModeIsDefaultBootstrapExecutorIsNotConfigured() {
120121
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
121122
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
122123
TaskSchedulingAutoConfiguration.class))
123124
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=default").run((context) -> assertThat(
124125
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull());
125126
}
126127

128+
@Test
129+
void bootstrapModeIsDeferredByDefault() {
130+
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
131+
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
132+
TaskSchedulingAutoConfiguration.class))
133+
.run((context) -> assertThat(
134+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor())
135+
.isEqualTo(context.getBean("applicationTaskExecutor")));
136+
}
137+
127138
@Configuration(proxyBeanMethods = false)
128139
@EnableScheduling
129140
@Import(TestConfiguration.class)

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
import org.springframework.context.annotation.ComponentScan.Filter;
3737
import org.springframework.core.annotation.AliasFor;
3838
import org.springframework.core.env.Environment;
39+
import org.springframework.data.repository.config.BootstrapMode;
3940
import org.springframework.test.context.BootstrapWith;
4041
import org.springframework.test.context.junit.jupiter.SpringExtension;
4142
import org.springframework.transaction.annotation.Transactional;
@@ -62,6 +63,7 @@
6263
*
6364
* @author Phillip Webb
6465
* @author Artsiom Yudovin
66+
* @author Scott Frederick
6567
* @since 1.4.0
6668
* @see AutoConfigureDataJpa
6769
* @see AutoConfigureTestDatabase
@@ -99,6 +101,14 @@
99101
@PropertyMapping("spring.jpa.show-sql")
100102
boolean showSql() default true;
101103

104+
/**
105+
* The {@link BootstrapMode} for the test repository support. Defaults to
106+
* {@link BootstrapMode#LAZY}.
107+
* @return the {@link BootstrapMode} to use for test the repository
108+
*/
109+
@PropertyMapping("spring.data.jpa.repositories.bootstrap-mode")
110+
BootstrapMode bootstrapMode() default BootstrapMode.LAZY;
111+
102112
/**
103113
* Determines if default filtering should be used with
104114
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,17 +20,18 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.core.env.Environment;
23+
import org.springframework.data.repository.config.BootstrapMode;
2324

2425
import static org.assertj.core.api.Assertions.assertThat;
2526

2627
/**
27-
* Tests for the {@link DataJpaTest#properties properties} attribute of
28-
* {@link DataJpaTest @DataJpaTest}.
28+
* Tests for non-default attributes of {@link DataJpaTest @DataJpaTest}.
2929
*
3030
* @author Artsiom Yudovin
31+
* @author Scott Frederick
3132
*/
32-
@DataJpaTest(properties = "spring.profiles.active=test")
33-
class DataJpaTestPropertiesIntegrationTests {
33+
@DataJpaTest(properties = "spring.profiles.active=test", bootstrapMode = BootstrapMode.DEFERRED)
34+
class DataJpaTestAttributesIntegrationTests {
3435

3536
@Autowired
3637
private Environment environment;
@@ -40,4 +41,10 @@ void environmentWithNewProfile() {
4041
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
4142
}
4243

44+
@Test
45+
void bootstrapModeIsSet() {
46+
assertThat(this.environment.getProperty("spring.data.jpa.repositories.bootstrap-mode"))
47+
.isEqualTo(BootstrapMode.DEFERRED.name());
48+
}
49+
4350
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
2727
import org.springframework.context.ApplicationContext;
28+
import org.springframework.data.repository.config.BootstrapMode;
2829
import org.springframework.jdbc.core.JdbcTemplate;
2930
import org.springframework.test.context.TestPropertySource;
3031

@@ -37,6 +38,7 @@
3738
*
3839
* @author Phillip Webb
3940
* @author Andy Wilkinson
41+
* @author Scott Frederick
4042
*/
4143
@DataJpaTest
4244
@TestPropertySource(properties = "spring.jpa.hibernate.use-new-id-generator-mappings=false")
@@ -106,4 +108,10 @@ void liquibaseAutoConfigurationWasImported() {
106108
assertThat(this.applicationContext).has(importedAutoConfiguration(LiquibaseAutoConfiguration.class));
107109
}
108110

111+
@Test
112+
void bootstrapModeIsLazyByDefault() {
113+
assertThat(this.applicationContext.getEnvironment().getProperty("spring.data.jpa.repositories.bootstrap-mode"))
114+
.isEqualTo(BootstrapMode.LAZY.name());
115+
}
116+
109117
}

0 commit comments

Comments
 (0)