From 106658fcf4ccd6b77643858d38167d35867eb541 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 10 Nov 2020 15:45:45 -0500 Subject: [PATCH] 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` 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 --- .../IntegrationManagementConfiguration.java | 5 ++--- .../IntegrationManagementConfigurer.java | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java index 3930b53730..2c1161dfe2 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,14 +70,13 @@ public class IntegrationManagementConfiguration implements ImportAware, Environm @Bean(name = IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public IntegrationManagementConfigurer managementConfigurer(ObjectProvider metricsCaptorProvider) { - IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer(); setupCountsEnabledNamePatterns(configurer); setupStatsEnabledNamePatterns(configurer); configurer.setDefaultLoggingEnabled( Boolean.parseBoolean(this.environment.resolvePlaceholders( (String) this.attributes.get("defaultLoggingEnabled")))); - configurer.setMetricsCaptor(metricsCaptorProvider.getIfUnique()); + configurer.setMetricsCaptorProvider(metricsCaptorProvider); configurer.setDefaultCountsEnabled( Boolean.parseBoolean(this.environment.resolvePlaceholders( (String) this.attributes.get("defaultCountsEnabled")))); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java index 4d15c79c73..283d50a9e3 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; import org.springframework.context.ApplicationContext; @@ -108,6 +109,8 @@ public class IntegrationManagementConfigurer private MetricsCaptor metricsCaptor; + private ObjectProvider metricsCaptorProvider; + @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; @@ -254,12 +257,24 @@ public class IntegrationManagementConfigurer this.metricsCaptor = metricsCaptor; } + void setMetricsCaptorProvider(ObjectProvider metricsCaptorProvider) { + this.metricsCaptorProvider = metricsCaptorProvider; + } + + @Nullable + MetricsCaptor obtainMetricsCaptor() { + if (this.metricsCaptor == null && this.metricsCaptorProvider != null) { + this.metricsCaptor = this.metricsCaptorProvider.getIfUnique(); + } + return this.metricsCaptor; + } + @Override public void afterSingletonsInstantiated() { Assert.state(this.applicationContext != null, "'applicationContext' must not be null"); Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName() + " bean name must be " + MANAGEMENT_CONFIGURER_NAME); - if (this.metricsCaptor != null) { + if (obtainMetricsCaptor() != null) { injectCaptor(); registerComponentGauges(); } @@ -308,7 +323,7 @@ public class IntegrationManagementConfigurer @Override public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { if (this.singletonsInstantiated) { - if (this.metricsCaptor != null && bean instanceof IntegrationManagement) { + if (obtainMetricsCaptor() != null && bean instanceof IntegrationManagement) { ((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor); } return doConfigureMetrics(bean, name);