diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessor.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessor.java index 18723f90c3..ae3e7f4954 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessor.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessor.java @@ -16,14 +16,13 @@ package org.springframework.boot.actuate.autoconfigure.metrics.data; -import java.util.function.Supplier; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.actuate.metrics.data.MetricsRepositoryMethodInvocationListener; import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer; import org.springframework.data.repository.core.support.RepositoryFactorySupport; +import org.springframework.util.function.SingletonSupplier; /** * {@link BeanPostProcessor} to apply a {@link MetricsRepositoryMethodInvocationListener} @@ -36,7 +35,7 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements Bean private final RepositoryFactoryCustomizer customizer; MetricsRepositoryMethodInvocationListenerBeanPostProcessor( - Supplier listener) { + SingletonSupplier listener) { this.customizer = new MetricsRepositoryFactoryCustomizer(listener); } @@ -50,21 +49,16 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements Bean private static final class MetricsRepositoryFactoryCustomizer implements RepositoryFactoryCustomizer { - private final Supplier listenerSupplier; - - private volatile MetricsRepositoryMethodInvocationListener listener; + private final SingletonSupplier listenerSupplier; private MetricsRepositoryFactoryCustomizer( - Supplier listenerSupplier) { + SingletonSupplier listenerSupplier) { this.listenerSupplier = listenerSupplier; } @Override public void customize(RepositoryFactorySupport repositoryFactory) { - if (this.listener == null) { - this.listener = this.listenerSupplier.get(); - } - repositoryFactory.addInvocationListener(this.listener); + repositoryFactory.addInvocationListener(this.listenerSupplier.get()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.java index b89546df22..62b1e004c8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.java @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.function.SingletonSupplier; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Repository metrics. @@ -75,7 +76,7 @@ public class RepositoryMetricsAutoConfiguration { public static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor( ObjectProvider metricsRepositoryMethodInvocationListener) { return new MetricsRepositoryMethodInvocationListenerBeanPostProcessor( - metricsRepositoryMethodInvocationListener::getObject); + SingletonSupplier.of(metricsRepositoryMethodInvocationListener::getObject)); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests.java index a7d12e6491..b3c528054f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests.java @@ -23,6 +23,7 @@ import org.springframework.boot.actuate.metrics.data.MetricsRepositoryMethodInvo import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer; import org.springframework.data.repository.core.support.RepositoryFactorySupport; +import org.springframework.util.function.SingletonSupplier; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -38,7 +39,7 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests { private MetricsRepositoryMethodInvocationListener listener = mock(MetricsRepositoryMethodInvocationListener.class); private MetricsRepositoryMethodInvocationListenerBeanPostProcessor postProcessor = new MetricsRepositoryMethodInvocationListenerBeanPostProcessor( - () -> this.listener); + SingletonSupplier.of(this.listener)); @Test @SuppressWarnings("rawtypes")