Skip to content

Scoped targets such as those created by @RefreshScope are not skipped when discovering endpoint beans #15182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.actuate.endpoint.EndpointFilter;
Expand Down Expand Up @@ -132,11 +133,14 @@ private Collection<EndpointBean> createEndpointBeans() {
this.applicationContext, Endpoint.class);
for (String beanName : beanNames) {
EndpointBean endpointBean = createEndpointBean(beanName);
EndpointBean previous = byId.putIfAbsent(endpointBean.getId(), endpointBean);
Assert.state(previous == null,
() -> "Found two endpoints with the id '" + endpointBean.getId()
+ "': '" + endpointBean.getBeanName() + "' and '"
+ previous.getBeanName() + "'");
if (!ScopedProxyUtils.isScopedTarget(beanName)) {
EndpointBean previous = byId.putIfAbsent(endpointBean.getId(),
endpointBean);
Assert.state(previous == null,
() -> "Found two endpoints with the id '" + endpointBean.getId()
+ "': '" + endpointBean.getBeanName() + "' and '"
+ previous.getBeanName() + "'");
}
}
return byId.values();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public void getEndpointsWhenTwoEndpointsHaveTheSameIdShouldThrowException() {
"Found two endpoints with the id 'test': "));
}

@Test
public void getEndpointsWhenEndpointsArePrefixedWithScopedTargetShouldRegisterOnlyOneEndpoint() {
load(ScopedTargetEndpointConfiguration.class, (context) -> {
Collection<TestExposableEndpoint> endpoints = new TestEndpointDiscoverer(
context).getEndpoints();
assertThat(endpoints).hasSize(1);
assertThat(endpoints.iterator().next().getEndpointBean()).isSameAs(context
.getBean(ScopedTargetEndpointConfiguration.class).testEndpoint());
});
}

@Test
public void getEndpointsWhenTtlSetToZeroShouldNotCacheInvokeCalls() {
load(TestEndpointConfiguration.class, (context) -> {
Expand Down Expand Up @@ -393,6 +404,21 @@ public TestEndpoint testEndpointOne() {

}

@Configuration
static class ScopedTargetEndpointConfiguration {

@Bean
public TestEndpoint testEndpoint() {
return new TestEndpoint();
}

@Bean(name = "scopedTarget.testEndpoint")
public TestEndpoint scopedTargetTestEndpoint() {
return new TestEndpoint();
}

}

@Import({ TestEndpoint.class, SpecializedTestEndpoint.class,
SpecializedExtension.class })
static class SpecializedEndpointsConfiguration {
Expand Down