Use SingletonSupplier in MetricsRepositoryFactoryCustomizer

Closes gh-27634
pull/28375/head
Andy Wilkinson 3 years ago
parent 35ea3b234d
commit 341e129a59

@ -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<MetricsRepositoryMethodInvocationListener> listener) {
SingletonSupplier<MetricsRepositoryMethodInvocationListener> listener) {
this.customizer = new MetricsRepositoryFactoryCustomizer(listener);
}
@ -50,21 +49,16 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements Bean
private static final class MetricsRepositoryFactoryCustomizer implements RepositoryFactoryCustomizer {
private final Supplier<MetricsRepositoryMethodInvocationListener> listenerSupplier;
private volatile MetricsRepositoryMethodInvocationListener listener;
private final SingletonSupplier<MetricsRepositoryMethodInvocationListener> listenerSupplier;
private MetricsRepositoryFactoryCustomizer(
Supplier<MetricsRepositoryMethodInvocationListener> listenerSupplier) {
SingletonSupplier<MetricsRepositoryMethodInvocationListener> 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());
}
}

@ -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> metricsRepositoryMethodInvocationListener) {
return new MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
metricsRepositoryMethodInvocationListener::getObject);
SingletonSupplier.of(metricsRepositoryMethodInvocationListener::getObject));
}
}

@ -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")

Loading…
Cancel
Save