Skip to content

Commit c6ce97b

Browse files
committed
Disable all mappings endpoint infra when endpoint is unavailable
Previously, when the mappings endpoint was not available, the beans that provide mapping descriptions were still created. This resulted in unnecessary CPU and memory usage collecting and storing information that would never by used. This commit updates the auto-configuration for the mappings endpoint so that all the beans that it creates are conditional on the endpoint being available, rather than only the endpoint bean itself. Closes gh-23977
1 parent 7592c98 commit c6ce97b

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/mappings/MappingsEndpointAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,10 +44,10 @@
4444
* @since 2.0.0
4545
*/
4646
@Configuration(proxyBeanMethods = false)
47+
@ConditionalOnAvailableEndpoint(endpoint = MappingsEndpoint.class)
4748
public class MappingsEndpointAutoConfiguration {
4849

4950
@Bean
50-
@ConditionalOnAvailableEndpoint
5151
public MappingsEndpoint mappingsEndpoint(ApplicationContext applicationContext,
5252
ObjectProvider<MappingDescriptionProvider> descriptionProviders) {
5353
return new MappingsEndpoint(descriptionProviders.orderedStream().collect(Collectors.toList()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.mappings;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
22+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration;
24+
import org.springframework.boot.actuate.web.mappings.MappingDescriptionProvider;
25+
import org.springframework.boot.actuate.web.mappings.MappingsEndpoint;
26+
import org.springframework.boot.autoconfigure.AutoConfigurations;
27+
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
32+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link MappingsEndpointAutoConfiguration}.
38+
*
39+
* @author Andy Wilkinson
40+
*/
41+
public class MappingsEndpointAutoConfigurationTests {
42+
43+
@Test
44+
public void whenEndpointIsUnavailableThenEndpointAndDescriptionProvidersAreNotCreated() {
45+
new WebApplicationContextRunner()
46+
.withConfiguration(AutoConfigurations.of(MappingsEndpointAutoConfiguration.class,
47+
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
48+
WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
49+
EndpointAutoConfiguration.class, WebEndpointAutoConfiguration.class,
50+
WebMvcEndpointManagementContextConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
51+
.run((context) -> {
52+
assertThat(context).doesNotHaveBean(MappingsEndpoint.class);
53+
assertThat(context).doesNotHaveBean(MappingDescriptionProvider.class);
54+
});
55+
56+
}
57+
58+
@Test
59+
public void whenEndpointIsAvailableThenEndpointAndDescriptionProvidersAreCreated() {
60+
new WebApplicationContextRunner()
61+
.withConfiguration(AutoConfigurations.of(MappingsEndpointAutoConfiguration.class,
62+
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
63+
WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
64+
EndpointAutoConfiguration.class, WebEndpointAutoConfiguration.class,
65+
WebMvcEndpointManagementContextConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
66+
.withPropertyValues("management.endpoints.web.exposure.include=mappings").run((context) -> {
67+
assertThat(context).hasSingleBean(MappingsEndpoint.class);
68+
assertThat(context.getBeansOfType(MappingDescriptionProvider.class)).hasSize(3);
69+
});
70+
71+
}
72+
73+
}

0 commit comments

Comments
 (0)