Skip to content

Commit 3e162a7

Browse files
author
Michael Wiles
committed
fix for #2962
with tests
1 parent 45fe5be commit 3e162a7

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

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

Lines changed: 16 additions & 11 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;
@@ -172,18 +174,21 @@ private void registerMessagePublishingErrorHandler() {
172174
private void registerNullChannel() {
173175
if (this.beanFactory.containsBean(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
174176
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);
177+
BeanFactory beanFactory = this.beanFactory;
178+
do {
179+
if (beanFactory instanceof ConfigurableListableBeanFactory) {
180+
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactory;
181+
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
182+
nullChannelDefinition = listable
183+
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
184+
}
185185
}
186-
}
186+
// don't think it will hurt to always find the parent
187+
if (beanFactory instanceof HierarchicalBeanFactory) {
188+
beanFactory = ((HierarchicalBeanFactory) beanFactory).getParentBeanFactory();
189+
}
190+
// will definitely be found as containsBean returned true - but also want to be defensive in case of NPE
191+
} while (nullChannelDefinition == null && beanFactory != null); // not sure if beanFactroy not null is necessary
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ public void testMessagingGateway() throws InterruptedException {
470470
assertThat(this.asyncAnnotationProcessThread.get()).isNotSameAs(Thread.currentThread());
471471
}
472472

473+
/**
474+
* Just creates an interim context to confirm that the DefaultConfiguringBeanFactoryPostProcessor
475+
* does not fail when there is an extra application context in the hierarchy.
476+
*/
477+
@Test
478+
public void testDoubleParentChildAnnotationConfiguration() {
479+
AnnotationConfigApplicationContext parent;
480+
{
481+
parent = new AnnotationConfigApplicationContext();
482+
parent.register(ChildConfiguration.class);
483+
parent.setParent(this.context);
484+
parent.refresh();
485+
}
486+
AnnotationConfigApplicationContext child;
487+
{
488+
child = new AnnotationConfigApplicationContext();
489+
child.register(ChildConfiguration.class);
490+
child.setParent(parent);
491+
child.refresh();
492+
}
493+
parent.close();
494+
child.close();
495+
}
496+
473497
@Test
474498
public void testParentChildAnnotationConfiguration() {
475499
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)