Skip to content

Commit 3668d9f

Browse files
michaelwilesartembilan
authored andcommitted
GH-2962: check the whole ctx hierarchy for nullChannel
Fixes #2962 * addressing code style removed comments and addressed review comments **Cherry-pick to 5.1.x**
1 parent 5ad4f62 commit 3668d9f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
import org.springframework.beans.BeansException;
3434
import org.springframework.beans.factory.BeanClassLoaderAware;
35+
import org.springframework.beans.factory.BeanFactory;
36+
import org.springframework.beans.factory.HierarchicalBeanFactory;
3537
import org.springframework.beans.factory.SmartInitializingSingleton;
3638
import org.springframework.beans.factory.config.BeanDefinition;
3739
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -79,6 +81,7 @@
7981
* @author Oleg Zhurakousky
8082
* @author Artem Bilan
8183
* @author Gary Russell
84+
* @author Michael Wiles
8285
*
8386
* @see IntegrationContextUtils
8487
*/
@@ -151,18 +154,20 @@ public void afterSingletonsInstantiated() {
151154
private void registerNullChannel() {
152155
if (this.beanFactory.containsBean(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
153156
BeanDefinition nullChannelDefinition = null;
154-
if (this.beanFactory.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
155-
nullChannelDefinition =
156-
this.beanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
157-
}
158-
else {
159-
BeanDefinitionRegistry parentBeanFactory =
160-
(BeanDefinitionRegistry) this.beanFactory.getParentBeanFactory();
161-
if (parentBeanFactory != null) {
162-
nullChannelDefinition =
163-
parentBeanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
157+
BeanFactory beanFactory = this.beanFactory;
158+
do {
159+
if (beanFactory instanceof ConfigurableListableBeanFactory) {
160+
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactory;
161+
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
162+
nullChannelDefinition = listable
163+
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
164+
}
165+
}
166+
if (beanFactory instanceof HierarchicalBeanFactory) {
167+
beanFactory = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
164168
}
165169
}
170+
while (nullChannelDefinition == null);
166171

167172
if (nullChannelDefinition != null &&
168173
!NullChannel.class.getName().equals(nullChannelDefinition.getBeanClassName())) {

spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import org.springframework.integration.config.GlobalChannelInterceptor;
105105
import org.springframework.integration.config.IntegrationConverter;
106106
import org.springframework.integration.config.SpelFunctionFactoryBean;
107+
import org.springframework.integration.context.IntegrationContextUtils;
107108
import org.springframework.integration.core.MessageSource;
108109
import org.springframework.integration.core.MessagingTemplate;
109110
import org.springframework.integration.endpoint.AbstractEndpoint;
@@ -149,6 +150,7 @@
149150
/**
150151
* @author Artem Bilan
151152
* @author Gary Russell
153+
* @author Michael Wiles
152154
*
153155
* @since 4.0
154156
*/
@@ -477,6 +479,30 @@ public void testMessagingGateway() throws InterruptedException {
477479
assertThat(this.asyncAnnotationProcessThread.get(), not(sameInstance(Thread.currentThread())));
478480
}
479481

482+
@Test
483+
public void testDoubleParentChildAnnotationConfiguration() {
484+
assertTrue(this.context.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
485+
486+
AnnotationConfigApplicationContext parent;
487+
parent = new AnnotationConfigApplicationContext();
488+
parent.register(ChildConfiguration.class);
489+
parent.setParent(this.context);
490+
parent.refresh();
491+
492+
assertFalse(parent.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
493+
494+
AnnotationConfigApplicationContext child;
495+
child = new AnnotationConfigApplicationContext();
496+
child.register(ChildConfiguration.class);
497+
child.setParent(parent);
498+
child.refresh();
499+
500+
assertFalse(child.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME));
501+
502+
parent.close();
503+
child.close();
504+
}
505+
480506
@Test
481507
public void testParentChildAnnotationConfiguration() {
482508
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)