Skip to content

Commit df2972e

Browse files
Fix some dependency issues with NonRootBeanPostProcessor
1 parent 5978835 commit df2972e

2 files changed

Lines changed: 42 additions & 30 deletions

File tree

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/NonRootBeanPostProcessor.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.BeansException;
3434
import org.springframework.beans.factory.BeanFactory;
3535
import org.springframework.beans.factory.BeanFactoryAware;
36+
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
3637
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3738
import org.springframework.beans.factory.config.BeanPostProcessor;
3839
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -45,26 +46,30 @@ public class NonRootBeanPostProcessor implements BeanPostProcessor, BeanFactoryA
4546

4647
private final @Nonnull TemporalProperties temporalProperties;
4748
private final @Nullable List<NonRootNamespaceProperties> namespaceProperties;
48-
private final @Nullable Tracer tracer;
49-
private final @Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment;
50-
private final @Nullable Scope metricsScope;
51-
52-
public NonRootBeanPostProcessor(
53-
@Nonnull TemporalProperties temporalProperties,
54-
@Nullable Tracer tracer,
55-
@Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
56-
@Nullable Scope metricsScope) {
49+
private @Nullable Tracer tracer;
50+
private @Nullable TestWorkflowEnvironmentAdapter testWorkflowEnvironment;
51+
private @Nullable Scope metricsScope;
52+
53+
public NonRootBeanPostProcessor(@Nonnull TemporalProperties temporalProperties) {
5754
this.temporalProperties = temporalProperties;
5855
this.namespaceProperties = temporalProperties.getNamespaces();
59-
this.tracer = tracer;
60-
this.testWorkflowEnvironment = testWorkflowEnvironment;
61-
this.metricsScope = metricsScope;
6256
}
6357

6458
@Override
65-
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
59+
public Object postProcessAfterInitialization(@Nonnull Object bean, @Nonnull String beanName)
60+
throws BeansException {
6661
if (bean instanceof NamespaceTemplate && beanName.equals("temporalRootNamespaceTemplate")) {
6762
if (namespaceProperties != null) {
63+
// If there are non-root namespaces, we need to inject beans for each of them. Look
64+
// up the bean manually instead of using @Autowired to avoid circular dependencies or causing the dependency to
65+
// get initialized to early and skip post-processing.
66+
//
67+
// Note: We don't use @Lazy here because these dependencies are optional and @Lazy doesn't interact well with
68+
// optional dependencies.
69+
metricsScope = findBean("temporalMetricsScope", Scope.class);
70+
tracer = findBean(Tracer.class);
71+
testWorkflowEnvironment =
72+
findBean("temporalTestWorkflowEnvironment", TestWorkflowEnvironmentAdapter.class);
6873
namespaceProperties.forEach(this::injectBeanByNonRootNamespace);
6974
}
7075
}
@@ -163,6 +168,24 @@ private <T> T findBeanByNamespace(String beanPrefix, Class<T> clazz) {
163168
return null;
164169
}
165170

171+
private <T> @Nullable T findBean(Class<T> clazz) {
172+
try {
173+
return beanFactory.getBean(clazz);
174+
} catch (NoSuchBeanDefinitionException | BeanNotOfRequiredTypeException ignore) {
175+
// Ignore if the bean is not found or not of the required type
176+
}
177+
return null;
178+
}
179+
180+
private <T> @Nullable T findBean(String beanName, Class<T> clazz) {
181+
try {
182+
return beanFactory.getBean(beanName, clazz);
183+
} catch (NoSuchBeanDefinitionException | BeanNotOfRequiredTypeException ignore) {
184+
// Ignore if the bean is not found or not of the required type
185+
}
186+
return null;
187+
}
188+
166189
private <T> TemporalOptionsCustomizer<T> findBeanByNameSpaceForTemporalCustomizer(
167190
String beanPrefix, Class<T> genericOptionsBuilderClass) {
168191
String beanName =

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/NonRootNamespaceAutoConfiguration.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package io.temporal.spring.boot.autoconfigure;
22

33
import com.google.common.base.MoreObjects;
4-
import com.uber.m3.tally.Scope;
5-
import io.opentracing.Tracer;
64
import io.temporal.spring.boot.autoconfigure.properties.NamespaceProperties;
75
import io.temporal.spring.boot.autoconfigure.properties.NonRootNamespaceProperties;
86
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
9-
import io.temporal.spring.boot.autoconfigure.template.TestWorkflowEnvironmentAdapter;
107
import io.temporal.spring.boot.autoconfigure.template.WorkersTemplate;
118
import java.util.List;
129
import java.util.Optional;
@@ -15,8 +12,6 @@
1512
import org.slf4j.Logger;
1613
import org.slf4j.LoggerFactory;
1714
import org.springframework.beans.BeansException;
18-
import org.springframework.beans.factory.annotation.Autowired;
19-
import org.springframework.beans.factory.annotation.Qualifier;
2015
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2116
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2217
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
@@ -36,27 +31,21 @@
3631
@AutoConfigureAfter({RootNamespaceAutoConfiguration.class, ServiceStubsAutoConfiguration.class})
3732
@ConditionalOnBean(ServiceStubsAutoConfiguration.class)
3833
@ConditionalOnExpression(
39-
"${spring.temporal.test-server.enabled:false} || '${spring.temporal.connection.target:}'.length() > 0")
34+
"(${spring.temporal.test-server.enabled:false} || '${spring.temporal.connection.target:}'.length() > 0) && ('${spring.temporal.namespaces:}'.length() > 0)")
4035
public class NonRootNamespaceAutoConfiguration {
4136

4237
protected static final Logger log =
4338
LoggerFactory.getLogger(NonRootNamespaceAutoConfiguration.class);
4439

4540
@Bean
46-
public NonRootBeanPostProcessor nonRootBeanPostProcessor(
47-
TemporalProperties properties,
48-
@Autowired(required = false) @Nullable Tracer otTracer,
49-
@Qualifier("temporalTestWorkflowEnvironmentAdapter") @Autowired(required = false) @Nullable
50-
TestWorkflowEnvironmentAdapter testWorkflowEnvironment,
51-
@Qualifier("temporalMetricsScope") @Autowired(required = false) @Nullable
52-
Scope metricsScope) {
53-
return new NonRootBeanPostProcessor(
54-
properties, otTracer, testWorkflowEnvironment, metricsScope);
41+
public static NonRootBeanPostProcessor nonRootBeanPostProcessor(
42+
@Lazy TemporalProperties properties) {
43+
return new NonRootBeanPostProcessor(properties);
5544
}
5645

5746
@Bean
58-
public NonRootNamespaceEventListener nonRootNamespaceEventListener(
59-
TemporalProperties temporalProperties,
47+
public static NonRootNamespaceEventListener nonRootNamespaceEventListener(
48+
@Lazy TemporalProperties temporalProperties,
6049
@Nullable @Lazy List<WorkersTemplate> workersTemplates) {
6150
return new NonRootNamespaceEventListener(temporalProperties, workersTemplates);
6251
}

0 commit comments

Comments
 (0)