From 3668d9f1d77a077e7d0d30862485a3411c895ca0 Mon Sep 17 00:00:00 2001 From: Michael Wiles Date: Thu, 13 Jun 2019 17:59:44 +0200 Subject: [PATCH] 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** --- ...ltConfiguringBeanFactoryPostProcessor.java | 25 +++++++++++------- .../configuration/EnableIntegrationTests.java | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java index b662d9b912..ec1dd50b43 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java @@ -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())) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java index a46ffb58ef..0160f77e42 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java @@ -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();