Commit 4853e6a7 authored by Rahul Ahuja's avatar Rahul Ahuja Committed by Phillip Webb

Skip scoped targets when determining endpoints

Update `EndpointDiscoverer` to filter out scoped target beans when
finding endpoints.

Closes gh-15182
parent 0bc45678
...@@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.actuate.endpoint.EndpointFilter; import org.springframework.boot.actuate.endpoint.EndpointFilter;
...@@ -131,11 +132,14 @@ public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O exten ...@@ -131,11 +132,14 @@ public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O exten
this.applicationContext, Endpoint.class); this.applicationContext, Endpoint.class);
for (String beanName : beanNames) { for (String beanName : beanNames) {
EndpointBean endpointBean = createEndpointBean(beanName); EndpointBean endpointBean = createEndpointBean(beanName);
EndpointBean previous = byId.putIfAbsent(endpointBean.getId(), endpointBean); if (!ScopedProxyUtils.isScopedTarget(beanName)) {
Assert.state(previous == null, EndpointBean previous = byId.putIfAbsent(endpointBean.getId(),
() -> "Found two endpoints with the id '" + endpointBean.getId() endpointBean);
+ "': '" + endpointBean.getBeanName() + "' and '" Assert.state(previous == null,
+ previous.getBeanName() + "'"); () -> "Found two endpoints with the id '" + endpointBean.getId()
+ "': '" + endpointBean.getBeanName() + "' and '"
+ previous.getBeanName() + "'");
}
} }
return byId.values(); return byId.values();
} }
......
...@@ -46,6 +46,7 @@ import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper; ...@@ -46,6 +46,7 @@ import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
import org.springframework.boot.actuate.endpoint.invoke.convert.ConversionServiceParameterValueMapper; import org.springframework.boot.actuate.endpoint.invoke.convert.ConversionServiceParameterValueMapper;
import org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvoker; import org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvoker;
import org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor; import org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor;
import org.springframework.boot.actuate.endpoint.jmx.EndpointMBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -148,6 +149,18 @@ public class EndpointDiscovererTests { ...@@ -148,6 +149,18 @@ public class EndpointDiscovererTests {
}); });
} }
@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 @Test
public void getEndpointsWhenTtlSetToZeroShouldNotCacheInvokeCalls() { public void getEndpointsWhenTtlSetToZeroShouldNotCacheInvokeCalls() {
load(TestEndpointConfiguration.class, (context) -> { load(TestEndpointConfiguration.class, (context) -> {
...@@ -393,6 +406,21 @@ public class EndpointDiscovererTests { ...@@ -393,6 +406,21 @@ public class EndpointDiscovererTests {
} }
@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, @Import({ TestEndpoint.class, SpecializedTestEndpoint.class,
SpecializedExtension.class }) SpecializedExtension.class })
static class SpecializedEndpointsConfiguration { static class SpecializedEndpointsConfiguration {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment