Remove dependant beans recursively

https://build.spring.io/browse/INT-MASTER-1058/

When we destroy manually registered `IntegrationFlow`, we need to
count with sub-flows and iterate all the dependant beans recursively
for full flow removal

**Cherry-pick to 5.0.x**
This commit is contained in:
Artem Bilan
2018-05-31 16:28:46 -04:00
committed by Gary Russell
parent 997b07afed
commit 94cded7575
2 changed files with 24 additions and 15 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.dsl.context;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -58,6 +57,8 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
private ConfigurableListableBeanFactory beanFactory;
private BeanDefinitionRegistry beanDefinitionRegistry;
private StandardIntegrationFlowContext() {
}
@@ -68,6 +69,7 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
"'ConfigurableListableBeanFactory'. " +
"Consider using 'GenericApplicationContext' implementation.");
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
this.beanDefinitionRegistry = (BeanDefinitionRegistry) this.beanFactory;
}
/**
@@ -163,17 +165,9 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
IntegrationFlowRegistration flowRegistration = this.registry.remove(flowId);
flowRegistration.stop();
BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) this.beanFactory;
removeDependantBeans(flowId);
Arrays.stream(this.beanFactory.getDependentBeans(flowId))
.forEach(beanName -> {
beanDefinitionRegistry.removeBeanDefinition(beanName);
// TODO until https://jira.spring.io/browse/SPR-16837
Arrays.asList(beanDefinitionRegistry.getAliases(beanName))
.forEach(beanDefinitionRegistry::removeAlias);
});
beanDefinitionRegistry.removeBeanDefinition(flowId);
this.beanDefinitionRegistry.removeBeanDefinition(flowId);
}
else {
throw new IllegalStateException("An IntegrationFlow with the id "
@@ -181,6 +175,19 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
}
}
private void removeDependantBeans(String parentName) {
String[] dependentBeans = this.beanFactory.getDependentBeans(parentName);
for (String beanName : dependentBeans) {
removeDependantBeans(beanName);
this.beanDefinitionRegistry.removeBeanDefinition(beanName);
// TODO until https://jira.spring.io/browse/SPR-16837
String[] aliases = this.beanDefinitionRegistry.getAliases(beanName);
for (String alias : aliases) {
this.beanDefinitionRegistry.removeAlias(alias);
}
}
}
/**
* Obtain a {@link MessagingTemplate} with its default destination set to the input channel
* of the {@link IntegrationFlow} for provided {@code flowId}.