Default factory for stubs when scanning as documented

Also add failing test. Fixes #172.
This commit is contained in:
Dave Syer
2025-05-15 17:43:43 +01:00
parent bd70fcd69f
commit 7de96df5bd
2 changed files with 31 additions and 2 deletions

View File

@@ -79,4 +79,29 @@ public class GrpcClientApplicationTests {
}
@Nested
@SpringBootTest
@AutoConfigureInProcessTransport
class ExplicitImportClientsWithNoFactory {
@Autowired
private ApplicationContext context;
@Test
void stubOfCorrectTypeIsCreated() {
assertThat(context.containsBeanDefinition("simpleBlockingStub")).isTrue();
assertThat(context.getBean(SimpleGrpc.SimpleBlockingStub.class)).isNotNull();
assertThat(context.containsBeanDefinition("simpleStub")).isFalse();
assertThat(context.containsBeanDefinition("simpleFutureStub")).isFalse();
assertThat(context.getBeanNamesForType(AbstractStub.class)).hasSize(1);
}
@TestConfiguration
@ImportGrpcClients
static class TestConfig {
}
}
}

View File

@@ -184,6 +184,9 @@ public class GrpcClientFactory {
private static boolean supports(Class<?> factory, Class<?> type) {
// To avoid needing to instantiate the factory we use reflection to check for a
// static supports() method. If it exists we call it.
if (factory == null) {
return false;
}
Method method = ReflectionUtils.findMethod(factory, "supports", Class.class);
boolean supports = false;
if (method != null) {
@@ -260,17 +263,18 @@ public class GrpcClientFactory {
Set<Class<?>> allTypes = new HashSet<>();
allTypes.addAll(Set.of(this.types));
for (String basePackage : this.packages) {
Class<?> factoryToUse = this.factory == null ? BlockingStubFactory.class : this.factory;
TypeFilter filter = new TypeFilter() {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
Class<?> type = ClassUtils.resolveClassName(metadataReader.getClassMetadata().getClassName(),
ClasspathScanner.class.getClassLoader());
return supports(GrpcClientRegistrationSpec.this.factory, type);
return supports(factoryToUse, type);
}
};
for (Class<?> type : SCANNER.scan(basePackage, filter)) {
if (findDefaultFactory(registry, this.factory, type) != null) {
if (findDefaultFactory(registry, factoryToUse, type) != null) {
allTypes.add(type);
}
}