Skip to content

Commit 169b701

Browse files
philwebbrstoyanchev
authored andcommitted
Only add httpServiceProxyRegistry bean when necessary
Update `AbstractHttpServiceRegistrar` so that the `httpServiceProxyRegistry` bean is only added when registrations are found.
1 parent fbe96a8 commit 169b701

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

spring-web/src/main/java/org/springframework/web/service/registry/AbstractHttpServiceRegistrar.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,22 @@ public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefin
147147

148148
registerHttpServices(new DefaultGroupRegistry(), metadata);
149149

150-
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
151-
152-
mergeGroups(proxyRegistryBeanDef);
153-
154-
this.groupsMetadata.forEachRegistration((groupName, types) -> types.forEach(type -> {
155-
RootBeanDefinition proxyBeanDef = new RootBeanDefinition();
156-
proxyBeanDef.setBeanClassName(type);
157-
proxyBeanDef.setAttribute(HTTP_SERVICE_GROUP_NAME_ATTRIBUTE, groupName);
158-
proxyBeanDef.setInstanceSupplier(() -> getProxyInstance(groupName, type));
159-
String beanName = (groupName + "#" + type);
160-
if (!beanRegistry.containsBeanDefinition(beanName)) {
161-
beanRegistry.registerBeanDefinition(beanName, proxyBeanDef);
162-
}
163-
}));
150+
if (this.groupsMetadata.hasRegistrations()) {
151+
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);
152+
153+
mergeGroups(proxyRegistryBeanDef);
154+
155+
this.groupsMetadata.forEachRegistration((groupName, types) -> types.forEach(type -> {
156+
RootBeanDefinition proxyBeanDef = new RootBeanDefinition();
157+
proxyBeanDef.setBeanClassName(type);
158+
proxyBeanDef.setAttribute(HTTP_SERVICE_GROUP_NAME_ATTRIBUTE, groupName);
159+
proxyBeanDef.setInstanceSupplier(() -> getProxyInstance(groupName, type));
160+
String beanName = (groupName + "#" + type);
161+
if (!beanRegistry.containsBeanDefinition(beanName)) {
162+
beanRegistry.registerBeanDefinition(beanName, proxyBeanDef);
163+
}
164+
}));
165+
}
164166
}
165167

166168
/**

spring-web/src/main/java/org/springframework/web/service/registry/GroupsMetadata.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ Stream<Registration> registrations() {
9797
return this.groupMap.values().stream();
9898
}
9999

100+
/**
101+
* Return if there are any {@link Registration registrations}.
102+
*/
103+
boolean hasRegistrations() {
104+
return !this.groupMap.isEmpty();
105+
}
106+
100107

101108
/**
102109
* Registration metadata for an {@link HttpServiceGroup}.

spring-web/src/test/java/org/springframework/web/service/registry/ClientHttpServiceRegistrarTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.Import;
2528
import org.springframework.core.env.StandardEnvironment;
2629
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
2730
import org.springframework.core.type.AnnotationMetadata;
@@ -63,6 +66,13 @@ protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata i
6366
TestGroup.ofListing("echo", EchoClientA.class, EchoClientB.class));
6467
}
6568

69+
@Test
70+
void registerWhenNoClientsDoesNotCreateBeans() {
71+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NothingFoundConfiguration.class)) {
72+
assertThat(context.getBeanNamesForType(HttpServiceProxyRegistry.class)).isEmpty();
73+
}
74+
}
75+
6676
private void assertGroups(TestGroup... expectedGroups) {
6777
Map<String, TestGroup> groupMap = this.groupRegistry.groupMap();
6878
assertThat(groupMap.size()).isEqualTo(expectedGroups.length);
@@ -75,4 +85,19 @@ private void assertGroups(TestGroup... expectedGroups) {
7585
}
7686
}
7787

88+
@Configuration(proxyBeanMethods = false)
89+
@Import(NothingFoundRegistrar.class)
90+
static class NothingFoundConfiguration {
91+
92+
}
93+
94+
static class NothingFoundRegistrar extends AbstractClientHttpServiceRegistrar {
95+
96+
@Override
97+
protected void registerHttpServices(GroupRegistry registry,
98+
AnnotationMetadata importingClassMetadata) {
99+
findAndRegisterHttpServiceClients(registry, List.of("com.example.missing.package"));
100+
}
101+
}
102+
78103
}

spring-web/src/test/java/org/springframework/web/service/registry/HttpServiceRegistrarTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ void defaultClientType() {
120120
@Test
121121
void noRegistrations() {
122122
doRegister(registry -> {});
123-
assertRegistryBeanDef();
124-
assertBeanDefinitionCount(1);
123+
assertBeanDefinitionCount(0);
125124
}
126125

127126

0 commit comments

Comments
 (0)