Skip to content

Commit 6ee8cde

Browse files
committed
Merge pull request #15565 from ayudovin
* pr/15565: Polish "Disable Hibernate entity scanning for default JPA setup" Disable Hibernate entity scanning for default JPA setup
2 parents 3354756 + d0811b4 commit 6ee8cde

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
2828
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
2929
import org.springframework.util.Assert;
30+
import org.springframework.util.ClassUtils;
3031
import org.springframework.util.ObjectUtils;
3132
import org.springframework.util.StringUtils;
3233

@@ -40,6 +41,8 @@
4041
@ConfigurationProperties("spring.jpa.hibernate")
4142
public class HibernateProperties {
4243

44+
private static final String DISABLED_SCANNER_CLASS = "org.hibernate.boot.archive.scan.internal.DisabledScanner";
45+
4346
private final Naming naming = new Naming();
4447

4548
/**
@@ -95,6 +98,7 @@ private Map<String, Object> getAdditionalProperties(Map<String, String> existing
9598
HibernateSettings settings) {
9699
Map<String, Object> result = new HashMap<>(existing);
97100
applyNewIdGeneratorMappings(result);
101+
applyScanner(result);
98102
getNaming().applyNamingStrategies(result);
99103
String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto);
100104
if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) {
@@ -121,6 +125,13 @@ else if (!result.containsKey(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS)) {
121125
}
122126
}
123127

128+
private void applyScanner(Map<String, Object> result) {
129+
if (!result.containsKey(AvailableSettings.SCANNER)
130+
&& ClassUtils.isPresent(DISABLED_SCANNER_CLASS, null)) {
131+
result.put(AvailableSettings.SCANNER, DISABLED_SCANNER_CLASS);
132+
}
133+
}
134+
124135
private String determineDdlAuto(Map<String, String> existing,
125136
Supplier<String> defaultDdlAuto) {
126137
String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* Tests for {@link HibernateProperties}.
4444
*
4545
* @author Stephane Nicoll
46+
* @author Artsiom Yudovin
4647
*/
4748
public class HibernatePropertiesTests {
4849

@@ -123,6 +124,23 @@ public void useNewIdGeneratorMappingsFalse() {
123124
"false")));
124125
}
125126

127+
@Test
128+
public void scannerUsesDisabledScannerByDefault() {
129+
this.contextRunner.run(assertHibernateProperties(
130+
(hibernateProperties) -> assertThat(hibernateProperties).containsEntry(
131+
AvailableSettings.SCANNER,
132+
"org.hibernate.boot.archive.scan.internal.DisabledScanner")));
133+
}
134+
135+
@Test
136+
public void scannerCanBeCustomized() {
137+
this.contextRunner.withPropertyValues(
138+
"spring.jpa.properties.hibernate.archive.scanner:org.hibernate.boot.archive.scan.internal.StandardScanner")
139+
.run(assertHibernateProperties((hibernateProperties) -> assertThat(
140+
hibernateProperties).containsEntry(AvailableSettings.SCANNER,
141+
"org.hibernate.boot.archive.scan.internal.StandardScanner")));
142+
}
143+
126144
@Test
127145
public void defaultDdlAutoIsNotInvokedIfPropertyIsSet() {
128146
this.contextRunner.withPropertyValues("spring.jpa.hibernate.ddl-auto=validate")

0 commit comments

Comments
 (0)