Skip to content

Commit fa7ed05

Browse files
committed
Add lazy getter for configurable bean factory
1 parent 025368f commit fa7ed05

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
6767
private List<StubTransformer> stubTransformers = null;
6868
private List<StubFactory> stubFactories = null;
6969

70+
// For bean registration via @GrpcClientBean
71+
private ConfigurableListableBeanFactory configurableBeanFactory;
72+
7073
/**
7174
* Creates a new GrpcClientBeanPostProcessor with the given ApplicationContext.
7275
*
@@ -105,8 +108,7 @@ public Object postProcessBeforeInitialization(final Object bean, final String be
105108
for (final GrpcClientBean beanClientIterator : clazz.getAnnotationsByType(GrpcClientBean.class)) {
106109
final String beanNameToCreate = getBeanName(beanClientIterator);
107110
try {
108-
final ConfigurableListableBeanFactory beanFactory =
109-
((ConfigurableApplicationContext) this.applicationContext).getBeanFactory();
111+
final ConfigurableListableBeanFactory beanFactory = getConfigurableBeanFactory();
110112
final Object beanValue =
111113
processInjectionPoint(null, beanClientIterator.clazz(), beanClientIterator.client());
112114
beanFactory.registerSingleton(beanNameToCreate, beanValue);
@@ -289,6 +291,18 @@ private List<StubFactory> getStubFactories() {
289291
return this.stubFactories;
290292
}
291293

294+
/**
295+
* Lazy factory getter from the context for bean registration with {@link GrpcClientBean} annotations.
296+
*
297+
* @return configurable bean factory
298+
*/
299+
private ConfigurableListableBeanFactory getConfigurableBeanFactory() {
300+
if (this.configurableBeanFactory == null) {
301+
this.configurableBeanFactory = ((ConfigurableApplicationContext) this.applicationContext).getBeanFactory();
302+
}
303+
return this.configurableBeanFactory;
304+
}
305+
292306
/**
293307
* The method is used to determine with which name the bean should be registered in the context
294308
*

0 commit comments

Comments
 (0)