Skip to content

Commit af725a8

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 72232ea commit af725a8

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;
@@ -105,6 +106,8 @@ public class IntegrationManagementConfigurer
105106

106107
private MetricsCaptor metricsCaptor;
107108

109+
private ObjectProvider<MetricsCaptor> metricsCaptorProvider;
110+
108111
@Override
109112
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
110113
this.applicationContext = applicationContext;
@@ -251,12 +254,24 @@ public void setMetricsCaptor(@Nullable MetricsCaptor metricsCaptor) {
251254
this.metricsCaptor = metricsCaptor;
252255
}
253256

257+
void setMetricsCaptorProvider(ObjectProvider<MetricsCaptor> metricsCaptorProvider) {
258+
this.metricsCaptorProvider = metricsCaptorProvider;
259+
}
260+
261+
@Nullable
262+
MetricsCaptor obtainMetricsCaptor() {
263+
if (this.metricsCaptor == null && this.metricsCaptorProvider != null) {
264+
this.metricsCaptor = this.metricsCaptorProvider.getIfUnique();
265+
}
266+
return this.metricsCaptor;
267+
}
268+
254269
@Override
255270
public void afterSingletonsInstantiated() {
256271
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
257272
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
258273
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
259-
if (this.metricsCaptor != null) {
274+
if (obtainMetricsCaptor() != null) {
260275
injectCaptor();
261276
registerComponentGauges();
262277
}
@@ -305,7 +320,7 @@ private void injectCaptor() {
305320
@Override
306321
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
307322
if (this.singletonsInstantiated) {
308-
if (this.metricsCaptor != null && bean instanceof IntegrationManagement) {
323+
if (obtainMetricsCaptor() != null && bean instanceof IntegrationManagement) {
309324
((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor);
310325
}
311326
return doConfigureMetrics(bean, name);

0 commit comments

Comments
 (0)