diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/client/AbstractGrpcClientRegistrar.java b/spring-grpc-core/src/main/java/org/springframework/grpc/client/AbstractGrpcClientRegistrar.java index fef3c26..81f5e62 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/client/AbstractGrpcClientRegistrar.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/client/AbstractGrpcClientRegistrar.java @@ -26,12 +26,12 @@ public abstract class AbstractGrpcClientRegistrar implements ImportBeanDefinitio @Override public final void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) { GrpcClientRegistrationSpec[] specs = collect(meta); - String name = GrpcClientFactoryPostProcessor.class.getName(); + String name = GrpcClientFactory.class.getName(); for (GrpcClientRegistrationSpec spec : specs) { GrpcClientFactory.register(registry, spec); } if (!registry.containsBeanDefinition(name)) { - registry.registerBeanDefinition(name, new RootBeanDefinition(GrpcClientFactoryPostProcessor.class)); + registry.registerBeanDefinition(name, new RootBeanDefinition(GrpcClientFactory.class)); } } 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 c6aa1b7..88b4a03 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 @@ -25,12 +25,14 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; @@ -48,7 +50,7 @@ import io.grpc.stub.AbstractStub; * * @author Dave Syer */ -public class GrpcClientFactory { +public class GrpcClientFactory implements ApplicationContextAware { private static final Set> DEFAULT_FACTORIES = new LinkedHashSet<>(); @@ -56,7 +58,7 @@ public class GrpcClientFactory { private Map, StubFactory> factories = new LinkedHashMap<>(); - private final ApplicationContext context; + private ApplicationContext context; static { DEFAULT_FACTORIES.add((Class>) BlockingStubFactory.class); @@ -66,8 +68,9 @@ public class GrpcClientFactory { DEFAULT_FACTORIES.add((Class>) SimpleStubFactory.class); } - public GrpcClientFactory(ApplicationContext context) { - this.context = context; + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context = applicationContext; } public T getClient(String target, Class type, Class factory) { @@ -213,7 +216,7 @@ public class GrpcClientFactory { } RootBeanDefinition beanDef = (RootBeanDefinition) BeanDefinitionBuilder.rootBeanDefinition(type) .setLazyInit(true) - .setFactoryMethodOnBean("getClient", GrpcClientFactoryPostProcessor.class.getName()) + .setFactoryMethodOnBean("getClient", GrpcClientFactory.class.getName()) .addConstructorArgValue(spec.target()) .addConstructorArgValue(type) .addConstructorArgValue(spec.factory()) diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactoryPostProcessor.java b/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactoryPostProcessor.java deleted file mode 100644 index 890335c..0000000 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/client/GrpcClientFactoryPostProcessor.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2024-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.grpc.client; - -import org.springframework.beans.BeansException; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - -/** - * Post processor for {@link GrpcClientFactory} that applies the customizers and provides - * a factory for client instances at runtime. - * - * @author Dave Syer - */ -public class GrpcClientFactoryPostProcessor implements ApplicationContextAware { - - private ApplicationContext context; - - private boolean initialized = false; - - private GrpcClientFactory registry; - - private void initialize(ApplicationContext context) { - if (this.initialized || this.context == null) { - return; - } - this.initialized = true; - this.registry = new GrpcClientFactory(context); - } - - T getClient(String target, Class type, Class factory) { - initialize(this.context); - return this.registry.getClient(target, (Class) type, factory); - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.context = applicationContext; - } - -} diff --git a/spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java b/spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java index 4488e4c..0c2f89d 100644 --- a/spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java +++ b/spring-grpc-core/src/test/java/org/springframework/grpc/client/GrpcClientFactoryTests.java @@ -48,7 +48,8 @@ public class GrpcClientFactoryTests { Mockito.when(channelFactory.createChannel(Mockito.anyString(), Mockito.any())) .thenReturn(Mockito.mock(ManagedChannel.class)); context.registerBean(GrpcChannelFactory.class, () -> channelFactory); - factory = new GrpcClientFactory(context); + factory = new GrpcClientFactory(); + factory.setApplicationContext(context); } @Test @@ -85,7 +86,8 @@ public class GrpcClientFactoryTests { context.registerBean(GrpcChannelFactory.class, () -> channelFactory); GrpcClientFactory.register(context, new GrpcClientRegistrationSpec("local", new Class[] { OtherStub.class })); context.refresh(); - factory = new GrpcClientFactory(context); + factory = new GrpcClientFactory(); + factory.setApplicationContext(context); assertThat(factory.getClient("local", OtherStub.class, null)).isNotNull(); } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java index 2ede10b..ac8f89a 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/ClientScanConfiguration.java @@ -33,11 +33,11 @@ import org.springframework.grpc.autoconfigure.client.ClientScanConfiguration.Def import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.ChannelConfig; import org.springframework.grpc.client.AbstractGrpcClientRegistrar; import org.springframework.grpc.client.BlockingStubFactory; +import org.springframework.grpc.client.GrpcClientFactory; import org.springframework.grpc.client.GrpcClientFactory.GrpcClientRegistrationSpec; -import org.springframework.grpc.client.GrpcClientFactoryPostProcessor; @Configuration(proxyBeanMethods = false) -@ConditionalOnMissingBean(GrpcClientFactoryPostProcessor.class) +@ConditionalOnMissingBean(GrpcClientFactory.class) @Import(DefaultGrpcClientRegistrations.class) public class ClientScanConfiguration {