Merge two classes into one
Fixes gh-166
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Class<?>> DEFAULT_FACTORIES = new LinkedHashSet<>();
|
||||
|
||||
@@ -56,7 +58,7 @@ public class GrpcClientFactory {
|
||||
|
||||
private Map<Class<?>, StubFactory<?>> factories = new LinkedHashMap<>();
|
||||
|
||||
private final ApplicationContext context;
|
||||
private ApplicationContext context;
|
||||
|
||||
static {
|
||||
DEFAULT_FACTORIES.add((Class<? extends StubFactory<?>>) BlockingStubFactory.class);
|
||||
@@ -66,8 +68,9 @@ public class GrpcClientFactory {
|
||||
DEFAULT_FACTORIES.add((Class<? extends StubFactory<?>>) SimpleStubFactory.class);
|
||||
}
|
||||
|
||||
public GrpcClientFactory(ApplicationContext context) {
|
||||
this.context = context;
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
public <T> T getClient(String target, Class<T> 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())
|
||||
|
||||
@@ -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> T getClient(String target, Class<T> type, Class<?> factory) {
|
||||
initialize(this.context);
|
||||
return this.registry.getClient(target, (Class<T>) type, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user