Skip to content

Commit 106658f

Browse files
committed
Fix mngmt dependency for MetricsCaptor
The `IntegrationManagementConfigurer` is a `BeanPostProcessor` so it must not have direct dependency injection for other beans. In our case it is a `MetricsCaptor` injected from the `IntegrationManagementConfiguration` * Fix `IntegrationManagementConfiguration` and `IntegrationManagementConfigurer` to rely on the `ObjectProvider<MetricsCaptor>` instead Tested against latest Spring Boot **Cherry-pick to `5.3.x` & `5.2.x`** # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java # spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java
1 parent fc5fdbf commit 106658f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,14 +70,13 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
7070
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
7171
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
7272
public IntegrationManagementConfigurer managementConfigurer(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
73-
7473
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
7574
setupCountsEnabledNamePatterns(configurer);
7675
setupStatsEnabledNamePatterns(configurer);
7776
configurer.setDefaultLoggingEnabled(
7877
Boolean.parseBoolean(this.environment.resolvePlaceholders(
7978
(String) this.attributes.get("defaultLoggingEnabled"))));
80-
configurer.setMetricsCaptor(metricsCaptorProvider.getIfUnique());
79+
configurer.setMetricsCaptorProvider(metricsCaptorProvider);
8180
configurer.setDefaultCountsEnabled(
8281
Boolean.parseBoolean(this.environment.resolvePlaceholders(
8382
(String) this.attributes.get("defaultCountsEnabled"))));

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.beans.BeansException;
3030
import org.springframework.beans.factory.BeanNameAware;
31+
import org.springframework.beans.factory.ObjectProvider;
3132
import org.springframework.beans.factory.SmartInitializingSingleton;
3233
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
3334
import org.springframework.context.ApplicationContext;
@@ -108,6 +109,8 @@ public class IntegrationManagementConfigurer
108109

109110
private MetricsCaptor metricsCaptor;
110111

112+
private ObjectProvider<MetricsCaptor> metricsCaptorProvider;
113+
111114
@Override
112115
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
113116
this.applicationContext = applicationContext;
@@ -254,12 +257,24 @@ public void setMetricsCaptor(@Nullable MetricsCaptor metricsCaptor) {
254257
this.metricsCaptor = metricsCaptor;
255258
}
256259

260+
void setMetricsCaptorProvider(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
261+
this.metricsCaptorProvider = metricsCaptorProvider;
262+
}
263+
264+
@Nullable
265+
MetricsCaptor obtainMetricsCaptor() {
266+
if (this.metricsCaptor == null && this.metricsCaptorProvider != null) {
267+
this.metricsCaptor = this.metricsCaptorProvider.getIfUnique();
268+
}
269+
return this.metricsCaptor;
270+
}
271+
257272
@Override
258273
public void afterSingletonsInstantiated() {
259274
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
260275
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
261276
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
262-
if (this.metricsCaptor != null) {
277+
if (obtainMetricsCaptor() != null) {
263278
injectCaptor();
264279
registerComponentGauges();
265280
}
@@ -308,7 +323,7 @@ private void injectCaptor() {
308323
@Override
309324
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
310325
if (this.singletonsInstantiated) {
311-
if (this.metricsCaptor != null && bean instanceof IntegrationManagement) {
326+
if (obtainMetricsCaptor() != null && bean instanceof IntegrationManagement) {
312327
((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor);
313328
}
314329
return doConfigureMetrics(bean, name);

0 commit comments

Comments
 (0)