GH-2962: check the whole ctx hierarchy for nullChannel

Fixes https://github.com/spring-projects/spring-integration/issues/2962

* addressing code style removed comments and addressed review comments

**Cherry-pick to 5.1.x**
This commit is contained in:
Michael Wiles
2019-06-13 17:59:44 +02:00
committed by Artem Bilan
parent 5ad4f62499
commit 3668d9f1d7
2 changed files with 41 additions and 10 deletions

View File

@@ -32,6 +32,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -79,6 +81,7 @@ import org.springframework.util.ClassUtils;
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gary Russell
* @author Michael Wiles
*
* @see IntegrationContextUtils
*/
@@ -151,18 +154,20 @@ class DefaultConfiguringBeanFactoryPostProcessor
private void registerNullChannel() {
if (this.beanFactory.containsBean(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
BeanDefinition nullChannelDefinition = null;
if (this.beanFactory.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
nullChannelDefinition =
this.beanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
}
else {
BeanDefinitionRegistry parentBeanFactory =
(BeanDefinitionRegistry) this.beanFactory.getParentBeanFactory();
if (parentBeanFactory != null) {
nullChannelDefinition =
parentBeanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
BeanFactory beanFactory = this.beanFactory;
do {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactory;
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
nullChannelDefinition = listable
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
}
}
if (beanFactory instanceof HierarchicalBeanFactory) {
beanFactory = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
}
}
while (nullChannelDefinition == null);
if (nullChannelDefinition != null &&
!NullChannel.class.getName().equals(nullChannelDefinition.getBeanClassName())) {

View File

@@ -104,6 +104,7 @@ import org.springframework.integration.config.ExpressionControlBusFactoryBean;
import org.springframework.integration.config.GlobalChannelInterceptor;
import org.springframework.integration.config.IntegrationConverter;
import org.springframework.integration.config.SpelFunctionFactoryBean;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.endpoint.AbstractEndpoint;
@@ -149,6 +150,7 @@ import reactor.core.publisher.Mono;
/**
* @author Artem Bilan
* @author Gary Russell
* @author Michael Wiles
*
* @since 4.0
*/
@@ -477,6 +479,30 @@ public class EnableIntegrationTests {
assertThat(this.asyncAnnotationProcessThread.get(), not(sameInstance(Thread.currentThread())));
}
@Test
public void testDoubleParentChildAnnotationConfiguration() {
assertTrue(this.context.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
AnnotationConfigApplicationContext parent;
parent = new AnnotationConfigApplicationContext();
parent.register(ChildConfiguration.class);
parent.setParent(this.context);
parent.refresh();
assertFalse(parent.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
AnnotationConfigApplicationContext child;
child = new AnnotationConfigApplicationContext();
child.register(ChildConfiguration.class);
child.setParent(parent);
child.refresh();
assertFalse(child.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
parent.close();
child.close();
}
@Test
public void testParentChildAnnotationConfiguration() {
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();