mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-24 07:39:09 +00:00
Only add httpServiceProxyRegistry bean when necessary
Update `AbstractHttpServiceRegistrar` so that the `httpServiceProxyRegistry` bean is only added when registrations are found.
This commit is contained in:
committed by
rstoyanchev
parent
fbe96a8112
commit
169b7015d2
+14
-12
@@ -147,20 +147,22 @@ public abstract class AbstractHttpServiceRegistrar implements
|
||||
|
||||
registerHttpServices(new DefaultGroupRegistry(), metadata);
|
||||
|
||||
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
|
||||
if (this.groupsMetadata.hasRegistrations()) {
|
||||
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
|
||||
|
||||
mergeGroups(proxyRegistryBeanDef);
|
||||
mergeGroups(proxyRegistryBeanDef);
|
||||
|
||||
this.groupsMetadata.forEachRegistration((groupName, types) -> types.forEach(type -> {
|
||||
RootBeanDefinition proxyBeanDef = new RootBeanDefinition();
|
||||
proxyBeanDef.setBeanClassName(type);
|
||||
proxyBeanDef.setAttribute(HTTP_SERVICE_GROUP_NAME_ATTRIBUTE, groupName);
|
||||
proxyBeanDef.setInstanceSupplier(() -> getProxyInstance(groupName, type));
|
||||
String beanName = (groupName + "#" + type);
|
||||
if (!beanRegistry.containsBeanDefinition(beanName)) {
|
||||
beanRegistry.registerBeanDefinition(beanName, proxyBeanDef);
|
||||
}
|
||||
}));
|
||||
this.groupsMetadata.forEachRegistration((groupName, types) -> types.forEach(type -> {
|
||||
RootBeanDefinition proxyBeanDef = new RootBeanDefinition();
|
||||
proxyBeanDef.setBeanClassName(type);
|
||||
proxyBeanDef.setAttribute(HTTP_SERVICE_GROUP_NAME_ATTRIBUTE, groupName);
|
||||
proxyBeanDef.setInstanceSupplier(() -> getProxyInstance(groupName, type));
|
||||
String beanName = (groupName + "#" + type);
|
||||
if (!beanRegistry.containsBeanDefinition(beanName)) {
|
||||
beanRegistry.registerBeanDefinition(beanName, proxyBeanDef);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -97,6 +97,13 @@ final class GroupsMetadata {
|
||||
return this.groupMap.values().stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if there are any {@link Registration registrations}.
|
||||
*/
|
||||
boolean hasRegistrations() {
|
||||
return !this.groupMap.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registration metadata for an {@link HttpServiceGroup}.
|
||||
|
||||
+25
@@ -22,6 +22,9 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
@@ -63,6 +66,13 @@ public class ClientHttpServiceRegistrarTests {
|
||||
TestGroup.ofListing("echo", EchoClientA.class, EchoClientB.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWhenNoClientsDoesNotCreateBeans() {
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NothingFoundConfiguration.class)) {
|
||||
assertThat(context.getBeanNamesForType(HttpServiceProxyRegistry.class)).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private void assertGroups(TestGroup... expectedGroups) {
|
||||
Map<String, TestGroup> groupMap = this.groupRegistry.groupMap();
|
||||
assertThat(groupMap.size()).isEqualTo(expectedGroups.length);
|
||||
@@ -75,4 +85,19 @@ public class ClientHttpServiceRegistrarTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(NothingFoundRegistrar.class)
|
||||
static class NothingFoundConfiguration {
|
||||
|
||||
}
|
||||
|
||||
static class NothingFoundRegistrar extends AbstractClientHttpServiceRegistrar {
|
||||
|
||||
@Override
|
||||
protected void registerHttpServices(GroupRegistry registry,
|
||||
AnnotationMetadata importingClassMetadata) {
|
||||
findAndRegisterHttpServiceClients(registry, List.of("com.example.missing.package"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -120,8 +120,7 @@ public class HttpServiceRegistrarTests {
|
||||
@Test
|
||||
void noRegistrations() {
|
||||
doRegister(registry -> {});
|
||||
assertRegistryBeanDef();
|
||||
assertBeanDefinitionCount(1);
|
||||
assertBeanDefinitionCount(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user