From 510a8e2ec16d93de743d2d475929ca3e66c3ec2a Mon Sep 17 00:00:00 2001 From: ayudovin Date: Tue, 25 Dec 2018 16:46:27 +0300 Subject: [PATCH 1/2] Disable Hibernate entity scanning for default JPA setup See gh-15565 --- .../orm/jpa/HibernateProperties.java | 9 +++++++++ .../orm/jpa/HibernatePropertiesTests.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java index d4100bcc350..4edd75154ff 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java @@ -34,6 +34,7 @@ import org.springframework.util.StringUtils; * Configuration properties for Hibernate. * * @author Stephane Nicoll + * @author Artsiom Yudovin * @since 2.1.0 * @see JpaProperties */ @@ -95,6 +96,7 @@ public class HibernateProperties { HibernateSettings settings) { Map result = new HashMap<>(existing); applyNewIdGeneratorMappings(result); + applyArchiveScanner(result); getNaming().applyNamingStrategies(result); String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto); if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) { @@ -121,6 +123,13 @@ public class HibernateProperties { } } + private void applyArchiveScanner(Map result) { + if (!result.containsKey(AvailableSettings.SCANNER)) { + result.put(AvailableSettings.SCANNER, + "org.hibernate.boot.archive.scan.internal.DisabledScanner"); + } + } + private String determineDdlAuto(Map existing, Supplier defaultDdlAuto) { String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java index 869769d7473..7dced9f747a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java @@ -43,6 +43,7 @@ import static org.mockito.Mockito.verify; * Tests for {@link HibernateProperties}. * * @author Stephane Nicoll + * @author Artsiom Yudovin */ public class HibernatePropertiesTests { @@ -123,6 +124,23 @@ public class HibernatePropertiesTests { "false"))); } + @Test + public void useArchiveScanner() { + this.contextRunner.withPropertyValues( + "spring.jpa.properties.hibernate.archive.scanner:org.hibernate.boot.archive.scan.internal.StandardScanner") + .run(assertHibernateProperties((hibernateProperties) -> assertThat( + hibernateProperties).containsEntry(AvailableSettings.SCANNER, + "org.hibernate.boot.archive.scan.internal.StandardScanner"))); + } + + @Test + public void defaultArchiveScanner() { + this.contextRunner.run(assertHibernateProperties( + (hibernateProperties) -> assertThat(hibernateProperties).containsEntry( + AvailableSettings.SCANNER, + "org.hibernate.boot.archive.scan.internal.DisabledScanner"))); + } + @Test public void defaultDdlAutoIsNotInvokedIfPropertyIsSet() { this.contextRunner.withPropertyValues("spring.jpa.hibernate.ddl-auto=validate") From d0811b48df6ff2d860b96f6540d45f9de53aecd6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 31 Dec 2018 16:51:43 +0100 Subject: [PATCH 2/2] Polish "Disable Hibernate entity scanning for default JPA setup" Closes gh-15565 --- .../orm/jpa/HibernateProperties.java | 14 ++++++++------ .../orm/jpa/HibernatePropertiesTests.java | 18 +++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java index 4edd75154ff..de2e24293b9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java @@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy; import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -34,13 +35,14 @@ import org.springframework.util.StringUtils; * Configuration properties for Hibernate. * * @author Stephane Nicoll - * @author Artsiom Yudovin * @since 2.1.0 * @see JpaProperties */ @ConfigurationProperties("spring.jpa.hibernate") public class HibernateProperties { + private static final String DISABLED_SCANNER_CLASS = "org.hibernate.boot.archive.scan.internal.DisabledScanner"; + private final Naming naming = new Naming(); /** @@ -96,7 +98,7 @@ public class HibernateProperties { HibernateSettings settings) { Map result = new HashMap<>(existing); applyNewIdGeneratorMappings(result); - applyArchiveScanner(result); + applyScanner(result); getNaming().applyNamingStrategies(result); String ddlAuto = determineDdlAuto(existing, settings::getDdlAuto); if (StringUtils.hasText(ddlAuto) && !"none".equals(ddlAuto)) { @@ -123,10 +125,10 @@ public class HibernateProperties { } } - private void applyArchiveScanner(Map result) { - if (!result.containsKey(AvailableSettings.SCANNER)) { - result.put(AvailableSettings.SCANNER, - "org.hibernate.boot.archive.scan.internal.DisabledScanner"); + private void applyScanner(Map result) { + if (!result.containsKey(AvailableSettings.SCANNER) + && ClassUtils.isPresent(DISABLED_SCANNER_CLASS, null)) { + result.put(AvailableSettings.SCANNER, DISABLED_SCANNER_CLASS); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java index 7dced9f747a..1340a3627f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java @@ -125,7 +125,15 @@ public class HibernatePropertiesTests { } @Test - public void useArchiveScanner() { + public void scannerUsesDisabledScannerByDefault() { + this.contextRunner.run(assertHibernateProperties( + (hibernateProperties) -> assertThat(hibernateProperties).containsEntry( + AvailableSettings.SCANNER, + "org.hibernate.boot.archive.scan.internal.DisabledScanner"))); + } + + @Test + public void scannerCanBeCustomized() { this.contextRunner.withPropertyValues( "spring.jpa.properties.hibernate.archive.scanner:org.hibernate.boot.archive.scan.internal.StandardScanner") .run(assertHibernateProperties((hibernateProperties) -> assertThat( @@ -133,14 +141,6 @@ public class HibernatePropertiesTests { "org.hibernate.boot.archive.scan.internal.StandardScanner"))); } - @Test - public void defaultArchiveScanner() { - this.contextRunner.run(assertHibernateProperties( - (hibernateProperties) -> assertThat(hibernateProperties).containsEntry( - AvailableSettings.SCANNER, - "org.hibernate.boot.archive.scan.internal.DisabledScanner"))); - } - @Test public void defaultDdlAutoIsNotInvokedIfPropertyIsSet() { this.contextRunner.withPropertyValues("spring.jpa.hibernate.ddl-auto=validate")