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
This commit is contained in:
Artem Bilan
2020-11-10 15:45:45 -05:00
parent fc5fdbf366
commit 106658fcf4
2 changed files with 19 additions and 5 deletions

View File

@@ -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<MetricsCaptor> 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"))));

View File

@@ -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<MetricsCaptor> 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<MetricsCaptor> 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);