Skip to content

Commit 360c740

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 d85d6ee commit 360c740

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
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;
@@ -83,6 +85,7 @@
8385
* @author Oleg Zhurakousky
8486
* @author Artem Bilan
8587
* @author Gary Russell
88+
* @author Michael Wiles
8689
*
8790
* @see IntegrationContextUtils
8891
*/
@@ -172,18 +175,20 @@ private void registerMessagePublishingErrorHandler() {
172175
private void registerNullChannel() {
173176
if (this.beanFactory.containsBean(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
174177
BeanDefinition nullChannelDefinition = null;
175-
if (this.beanFactory.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
176-
nullChannelDefinition =
177-
this.beanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
178-
}
179-
else {
180-
BeanDefinitionRegistry parentBeanFactory =
181-
(BeanDefinitionRegistry) this.beanFactory.getParentBeanFactory();
182-
if (parentBeanFactory != null) {
183-
nullChannelDefinition =
184-
parentBeanFactory.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
178+
BeanFactory beanFactory = this.beanFactory;
179+
do {
180+
if (beanFactory instanceof ConfigurableListableBeanFactory) {
181+
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactory;
182+
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
183+
nullChannelDefinition = listable
184+
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
185+
}
186+
}
187+
if (beanFactory instanceof HierarchicalBeanFactory) {
188+
beanFactory = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
185189
}
186190
}
191+
while (nullChannelDefinition == null);
187192

188193
if (nullChannelDefinition != null &&
189194
!NullChannel.class.getName().equals(nullChannelDefinition.getBeanClassName())) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import org.springframework.integration.config.GlobalChannelInterceptor;
9191
import org.springframework.integration.config.IntegrationConverter;
9292
import org.springframework.integration.config.SpelFunctionFactoryBean;
93+
import org.springframework.integration.context.IntegrationContextUtils;
9394
import org.springframework.integration.core.MessageSource;
9495
import org.springframework.integration.core.MessagingTemplate;
9596
import org.springframework.integration.endpoint.AbstractEndpoint;
@@ -136,6 +137,7 @@
136137
/**
137138
* @author Artem Bilan
138139
* @author Gary Russell
140+
* @author Michael Wiles
139141
*
140142
* @since 4.0
141143
*/
@@ -470,6 +472,36 @@ public void testMessagingGateway() throws InterruptedException {
470472
assertThat(this.asyncAnnotationProcessThread.get()).isNotSameAs(Thread.currentThread());
471473
}
472474

475+
/**
476+
* Just creates an interim context to confirm that the
477+
* DefaultConfiguringBeanFactoryPostProcessor does not fail when there is an extra
478+
* application context in the hierarchy.
479+
*/
480+
@Test
481+
public void testDoubleParentChildAnnotationConfiguration() {
482+
483+
assertThat(this.context.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)).isTrue();
484+
485+
AnnotationConfigApplicationContext parent;
486+
parent = new AnnotationConfigApplicationContext();
487+
parent.register(ChildConfiguration.class);
488+
parent.setParent(this.context);
489+
parent.refresh();
490+
491+
assertThat(parent.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)).isFalse();
492+
493+
AnnotationConfigApplicationContext child;
494+
child = new AnnotationConfigApplicationContext();
495+
child.register(ChildConfiguration.class);
496+
child.setParent(parent);
497+
child.refresh();
498+
499+
assertThat(child.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)).isFalse();
500+
501+
parent.close();
502+
child.close();
503+
}
504+
473505
@Test
474506
public void testParentChildAnnotationConfiguration() {
475507
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)