Skip to content

Commit 5183dd1

Browse files
committed
Merge pull request #15182 from rahul404
* pr/15182: Polish "Skip scoped targets when determining endpoints" Skip scoped targets when determining endpoints
2 parents 0bc4567 + e4d5714 commit 5183dd1

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Supplier;
3030
import java.util.stream.Collectors;
3131

32+
import org.springframework.aop.scope.ScopedProxyUtils;
3233
import org.springframework.beans.BeanUtils;
3334
import org.springframework.beans.factory.BeanFactoryUtils;
3435
import org.springframework.boot.actuate.endpoint.EndpointFilter;
@@ -130,12 +131,15 @@ private Collection<EndpointBean> createEndpointBeans() {
130131
String[] beanNames = BeanFactoryUtils.beanNamesForAnnotationIncludingAncestors(
131132
this.applicationContext, Endpoint.class);
132133
for (String beanName : beanNames) {
133-
EndpointBean endpointBean = createEndpointBean(beanName);
134-
EndpointBean previous = byId.putIfAbsent(endpointBean.getId(), endpointBean);
135-
Assert.state(previous == null,
136-
() -> "Found two endpoints with the id '" + endpointBean.getId()
137-
+ "': '" + endpointBean.getBeanName() + "' and '"
138-
+ previous.getBeanName() + "'");
134+
if (!ScopedProxyUtils.isScopedTarget(beanName)) {
135+
EndpointBean endpointBean = createEndpointBean(beanName);
136+
EndpointBean previous = byId.putIfAbsent(endpointBean.getId(),
137+
endpointBean);
138+
Assert.state(previous == null,
139+
() -> "Found two endpoints with the id '" + endpointBean.getId()
140+
+ "': '" + endpointBean.getBeanName() + "' and '"
141+
+ previous.getBeanName() + "'");
142+
}
139143
}
140144
return byId.values();
141145
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ public void getEndpointsWhenTwoEndpointsHaveTheSameIdShouldThrowException() {
148148
});
149149
}
150150

151+
@Test
152+
public void getEndpointsWhenEndpointsArePrefixedWithScopedTargetShouldRegisterOnlyOneEndpoint() {
153+
load(ScopedTargetEndpointConfiguration.class, (context) -> {
154+
TestEndpoint expectedEndpoint = context
155+
.getBean(ScopedTargetEndpointConfiguration.class).testEndpoint();
156+
Collection<TestExposableEndpoint> endpoints = new TestEndpointDiscoverer(
157+
context).getEndpoints();
158+
assertThat(endpoints).flatExtracting(TestExposableEndpoint::getEndpointBean)
159+
.containsOnly(expectedEndpoint);
160+
});
161+
}
162+
151163
@Test
152164
public void getEndpointsWhenTtlSetToZeroShouldNotCacheInvokeCalls() {
153165
load(TestEndpointConfiguration.class, (context) -> {
@@ -393,6 +405,21 @@ public TestEndpoint testEndpointOne() {
393405

394406
}
395407

408+
@Configuration
409+
static class ScopedTargetEndpointConfiguration {
410+
411+
@Bean
412+
public TestEndpoint testEndpoint() {
413+
return new TestEndpoint();
414+
}
415+
416+
@Bean(name = "scopedTarget.testEndpoint")
417+
public TestEndpoint scopedTargetTestEndpoint() {
418+
return new TestEndpoint();
419+
}
420+
421+
}
422+
396423
@Import({ TestEndpoint.class, SpecializedTestEndpoint.class,
397424
SpecializedExtension.class })
398425
static class SpecializedEndpointsConfiguration {

0 commit comments

Comments
 (0)