Skip to content

Only add httpServiceProxyRegistry bean when necessary #35307

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
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 @@ -147,20 +147,22 @@ public final void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefin

registerHttpServices(new DefaultGroupRegistry(), metadata);

RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);

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);
}
}));
if (this.groupsMetadata.hasRegistrations()) {
RootBeanDefinition proxyRegistryBeanDef = createOrGetRegistry(beanRegistry);

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);
}
}));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ Stream<Registration> registrations() {
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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

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;
Expand Down Expand Up @@ -63,6 +66,13 @@ protected void registerHttpServices(GroupRegistry registry, AnnotationMetadata i
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);
Expand All @@ -75,4 +85,19 @@ private void assertGroups(TestGroup... expectedGroups) {
}
}

@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"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ void defaultClientType() {
@Test
void noRegistrations() {
doRegister(registry -> {});
assertRegistryBeanDef();
assertBeanDefinitionCount(1);
assertBeanDefinitionCount(0);
}


Expand Down