Skip to content

Commit 6565b7c

Browse files
artembilangaryrussell
authored andcommitted
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`**
1 parent fce0fef commit 6565b7c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,11 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
6565
@Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
6666
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
6767
public IntegrationManagementConfigurer managementConfigurer(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
68-
6968
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
7069
configurer.setDefaultLoggingEnabled(
7170
Boolean.parseBoolean(this.environment.resolvePlaceholders(
7271
(String) this.attributes.get("defaultLoggingEnabled"))));
73-
configurer.setMetricsCaptor(metricsCaptorProvider.getIfUnique());
72+
configurer.setMetricsCaptorProvider(metricsCaptorProvider);
7473
return configurer;
7574
}
7675

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
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.beans.BeansException;
2525
import org.springframework.beans.factory.BeanNameAware;
26+
import org.springframework.beans.factory.ObjectProvider;
2627
import org.springframework.beans.factory.SmartInitializingSingleton;
2728
import org.springframework.beans.factory.config.BeanPostProcessor;
2829
import org.springframework.context.ApplicationContext;
@@ -73,6 +74,8 @@ public class IntegrationManagementConfigurer
7374

7475
private MetricsCaptor metricsCaptor;
7576

77+
private ObjectProvider<MetricsCaptor> metricsCaptorProvider;
78+
7679
@Override
7780
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
7881
this.applicationContext = applicationContext;
@@ -110,12 +113,24 @@ public void setMetricsCaptor(@Nullable MetricsCaptor metricsCaptor) {
110113
this.metricsCaptor = metricsCaptor;
111114
}
112115

116+
void setMetricsCaptorProvider(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
117+
this.metricsCaptorProvider = metricsCaptorProvider;
118+
}
119+
120+
@Nullable
121+
MetricsCaptor obtainMetricsCaptor() {
122+
if (this.metricsCaptor == null && this.metricsCaptorProvider != null) {
123+
this.metricsCaptor = this.metricsCaptorProvider.getIfUnique();
124+
}
125+
return this.metricsCaptor;
126+
}
127+
113128
@Override
114129
public void afterSingletonsInstantiated() {
115130
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
116131
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
117132
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
118-
if (this.metricsCaptor != null) {
133+
if (obtainMetricsCaptor() != null) {
119134
injectCaptor();
120135
registerComponentGauges();
121136
}
@@ -144,7 +159,7 @@ private void injectCaptor() {
144159

145160
@Override
146161
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
147-
if (this.singletonsInstantiated && this.metricsCaptor != null && bean instanceof IntegrationManagement) {
162+
if (this.singletonsInstantiated && obtainMetricsCaptor() != null && bean instanceof IntegrationManagement) {
148163
((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor);
149164
}
150165
return bean;

0 commit comments

Comments
 (0)