From 24dc525127a4cd9fa7392f7455211a02f9b1c06d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 23 May 2022 12:49:09 +0100 Subject: [PATCH] Fix conditions on auto-configured WebMvcMetricsFilter Closes gh-31150 --- .../WebMvcMetricsAutoConfiguration.java | 3 +- .../WebMvcMetricsAutoConfigurationTests.java | 65 +++++++++++++++++-- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java index 078f4cfbf2..23ab645bfb 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.java @@ -41,6 +41,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; @@ -80,7 +81,7 @@ public class WebMvcMetricsAutoConfiguration { } @Bean - @ConditionalOnMissingBean + @ConditionalOnMissingFilterBean public FilterRegistrationBean webMvcMetricsFilter(MeterRegistry registry, WebMvcTagsProvider tagsProvider) { ServerRequest request = this.properties.getWeb().getServer().getRequest(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java index 70dd128b3f..5eb2d5c56f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java @@ -122,12 +122,32 @@ class WebMvcMetricsAutoConfigurationTests { } @Test - void filterRegistrationBacksOff() { - this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterConfiguration.class).run((context) -> { - assertThat(context).hasSingleBean(FilterRegistrationBean.class); - assertThat(context.getBean(FilterRegistrationBean.class)) - .isSameAs(context.getBean("testWebMvcMetricsFilter")); - }); + void filterRegistrationBacksOffWithAnotherWebMvcMetricsFilterRegistration() { + this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterRegistrationConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(FilterRegistrationBean.class); + assertThat(context.getBean(FilterRegistrationBean.class)) + .isSameAs(context.getBean("testWebMvcMetricsFilter")); + }); + } + + @Test + void filterRegistrationBacksOffWithAnotherWebMvcMetricsFilter() { + this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterConfiguration.class) + .run((context) -> assertThat(context).doesNotHaveBean(FilterRegistrationBean.class) + .hasSingleBean(WebMvcMetricsFilter.class)); + } + + @Test + void filterRegistrationDoesNotBackOffWithOtherFilterRegistration() { + this.contextRunner.withUserConfiguration(TestFilterRegistrationConfiguration.class) + .run((context) -> assertThat(context).hasBean("testFilter").hasBean("webMvcMetricsFilter")); + } + + @Test + void filterRegistrationDoesNotBackOffWithOtherFilter() { + this.contextRunner.withUserConfiguration(TestFilterConfiguration.class) + .run((context) -> assertThat(context).hasBean("testFilter").hasBean("webMvcMetricsFilter")); } @Test @@ -258,7 +278,7 @@ class WebMvcMetricsAutoConfigurationTests { } @Configuration(proxyBeanMethods = false) - static class TestWebMvcMetricsFilterConfiguration { + static class TestWebMvcMetricsFilterRegistrationConfiguration { @Bean @SuppressWarnings("unchecked") @@ -268,4 +288,35 @@ class WebMvcMetricsAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class TestWebMvcMetricsFilterConfiguration { + + @Bean + WebMvcMetricsFilter testWebMvcMetricsFilter() { + return new WebMvcMetricsFilter(null, null, null, null); + } + + } + + @Configuration(proxyBeanMethods = false) + static class TestFilterRegistrationConfiguration { + + @Bean + @SuppressWarnings("unchecked") + FilterRegistrationBean testFilter() { + return mock(FilterRegistrationBean.class); + } + + } + + @Configuration(proxyBeanMethods = false) + static class TestFilterConfiguration { + + @Bean + Filter testFilter() { + return mock(Filter.class); + } + + } + }