diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfiguration.java index 9009e5f863..68c6c5122d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfiguration.java @@ -23,6 +23,7 @@ import javax.persistence.EntityManagerFactory; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.jpa.HibernateMetrics; +import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; @@ -46,7 +47,8 @@ import org.springframework.util.StringUtils; @Configuration @AutoConfigureAfter({ MetricsAutoConfiguration.class, HibernateJpaAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class }) -@ConditionalOnClass({ EntityManagerFactory.class, MeterRegistry.class }) +@ConditionalOnClass({ EntityManagerFactory.class, SessionFactory.class, + MeterRegistry.class }) @ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class }) public class HibernateMetricsAutoConfiguration { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfigurationTests.java index cc207e72a1..eab78c6dbb 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfigurationTests.java @@ -37,6 +37,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -53,6 +54,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link HibernateMetricsAutoConfiguration}. * * @author Rui Figueira + * @author Stephane Nicoll */ public class HibernateMetricsAutoConfigurationTests { @@ -137,6 +139,19 @@ public class HibernateMetricsAutoConfigurationTests { }); } + @Test + public void entityManagerFactoryInstrumentationIsDisabledIfHibernateIsNotAvailable() { + this.contextRunner.withClassLoader(new FilteredClassLoader(SessionFactory.class)) + .withUserConfiguration( + NonHibernateEntityManagerFactoryConfiguration.class) + .run((context) -> { + assertThat(context) + .doesNotHaveBean(HibernateMetricsAutoConfiguration.class); + MeterRegistry registry = context.getBean(MeterRegistry.class); + assertThat(registry.find("hibernate.statements").meter()).isNull(); + }); + } + @Configuration static class BaseConfiguration {