Merge branch '2.5.x'

Closes gh-27637
pull/27659/head
Andy Wilkinson 3 years ago
commit 52db64daa4

@ -16,14 +16,13 @@
package org.springframework.boot.actuate.autoconfigure.metrics.data; package org.springframework.boot.actuate.autoconfigure.metrics.data;
import java.util.function.Supplier;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.metrics.data.MetricsRepositoryMethodInvocationListener; import org.springframework.boot.actuate.metrics.data.MetricsRepositoryMethodInvocationListener;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer; import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer;
import org.springframework.data.repository.core.support.RepositoryFactorySupport; import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.function.SingletonSupplier;
/** /**
* {@link BeanPostProcessor} to apply a {@link MetricsRepositoryMethodInvocationListener} * {@link BeanPostProcessor} to apply a {@link MetricsRepositoryMethodInvocationListener}
@ -36,7 +35,7 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements Bean
private final RepositoryFactoryCustomizer customizer; private final RepositoryFactoryCustomizer customizer;
MetricsRepositoryMethodInvocationListenerBeanPostProcessor( MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
Supplier<MetricsRepositoryMethodInvocationListener> listener) { SingletonSupplier<MetricsRepositoryMethodInvocationListener> listener) {
this.customizer = new MetricsRepositoryFactoryCustomizer(listener); this.customizer = new MetricsRepositoryFactoryCustomizer(listener);
} }
@ -50,21 +49,16 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements Bean
private static final class MetricsRepositoryFactoryCustomizer implements RepositoryFactoryCustomizer { private static final class MetricsRepositoryFactoryCustomizer implements RepositoryFactoryCustomizer {
private final Supplier<MetricsRepositoryMethodInvocationListener> listenerSupplier; private final SingletonSupplier<MetricsRepositoryMethodInvocationListener> listenerSupplier;
private volatile MetricsRepositoryMethodInvocationListener listener;
private MetricsRepositoryFactoryCustomizer( private MetricsRepositoryFactoryCustomizer(
Supplier<MetricsRepositoryMethodInvocationListener> listenerSupplier) { SingletonSupplier<MetricsRepositoryMethodInvocationListener> listenerSupplier) {
this.listenerSupplier = listenerSupplier; this.listenerSupplier = listenerSupplier;
} }
@Override @Override
public void customize(RepositoryFactorySupport repositoryFactory) { public void customize(RepositoryFactorySupport repositoryFactory) {
if (this.listener == null) { repositoryFactory.addInvocationListener(this.listenerSupplier.get());
this.listener = this.listenerSupplier.get();
}
repositoryFactory.addInvocationListener(this.listener);
} }
} }

@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.function.SingletonSupplier;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Repository metrics. * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Repository metrics.
@ -75,7 +76,7 @@ public class RepositoryMetricsAutoConfiguration {
public static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor( public static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor(
ObjectProvider<MetricsRepositoryMethodInvocationListener> metricsRepositoryMethodInvocationListener) { ObjectProvider<MetricsRepositoryMethodInvocationListener> metricsRepositoryMethodInvocationListener) {
return new MetricsRepositoryMethodInvocationListenerBeanPostProcessor( 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.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer; import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer;
import org.springframework.data.repository.core.support.RepositoryFactorySupport; 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.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -38,7 +39,7 @@ class MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests {
private MetricsRepositoryMethodInvocationListener listener = mock(MetricsRepositoryMethodInvocationListener.class); private MetricsRepositoryMethodInvocationListener listener = mock(MetricsRepositoryMethodInvocationListener.class);
private MetricsRepositoryMethodInvocationListenerBeanPostProcessor postProcessor = new MetricsRepositoryMethodInvocationListenerBeanPostProcessor( private MetricsRepositoryMethodInvocationListenerBeanPostProcessor postProcessor = new MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
() -> this.listener); SingletonSupplier.of(this.listener));
@Test @Test
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")

Loading…
Cancel
Save