diff --git a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcClientApplicationTests.java b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcClientApplicationTests.java index 4b16fd8..4b96c30 100644 --- a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcClientApplicationTests.java +++ b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcClientApplicationTests.java @@ -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 { + + } + + } + } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java index 4b0749b..e5a07a5 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactory.java @@ -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> 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); } }