Skip scoped targets when determining endpoints

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

Closes gh-15182
This commit is contained in:
Rahul Ahuja
2018-11-15 20:12:06 +05:30
committed by Phillip Webb
parent 0bc4567898
commit 4853e6a7af
2 changed files with 37 additions and 5 deletions

View File

@@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentHashMap;
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;
@@ -131,11 +132,14 @@ public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O exten
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();
}

View File

@@ -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.invoker.cache.CachingOperationInvoker;
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.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -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
public void getEndpointsWhenTtlSetToZeroShouldNotCacheInvokeCalls() {
load(TestEndpointConfiguration.class, (context) -> {
@@ -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,
SpecializedExtension.class })
static class SpecializedEndpointsConfiguration {