From 181b1b9c5fc0a89a4f519e72104f94ae660c014a Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 18 Feb 2014 13:38:56 +0200 Subject: [PATCH] INT-3300: Fix `MS` ref Regression for i-c-adapter JIRA: https://jira.springsource.org/browse/INT-3300 The change introduced by https://jira.springsource.org/browse/INT-3147 (Registration of `MessageSource` as a bean with suffix `.source`) broke the case where `MessageSource` is a reference to an existing bean. Add check to the `AbstractPollingInboundChannelAdapterParser` for the type of `source` and apply the appropriate logic. Conflicts: spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java Resolved. --- ...ractPollingInboundChannelAdapterParser.java | 17 ++++++++++++++--- .../ChannelAdapterParserTests-context.xml | 8 ++++++++ .../config/ChannelAdapterParserTests.java | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractPollingInboundChannelAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractPollingInboundChannelAdapterParser.java index fd1771761b..a1f4308413 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractPollingInboundChannelAdapterParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractPollingInboundChannelAdapterParser.java @@ -20,6 +20,7 @@ import org.w3c.dom.Element; import org.springframework.beans.BeanMetadataElement; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; @@ -45,9 +46,19 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder .genericBeanDefinition(SourcePollingChannelAdapterFactoryBean.class); - String channelAdapterId = this.resolveId(element, adapterBuilder.getRawBeanDefinition(), parserContext); - String sourceBeanName = channelAdapterId + ".source"; - parserContext.getRegistry().registerBeanDefinition(sourceBeanName, (BeanDefinition) source); + String sourceBeanName = null; + + if (source instanceof BeanDefinition) { + String channelAdapterId = this.resolveId(element, adapterBuilder.getRawBeanDefinition(), parserContext); + sourceBeanName = channelAdapterId + ".source"; + parserContext.getRegistry().registerBeanDefinition(sourceBeanName, (BeanDefinition) source); + } + else if (source instanceof RuntimeBeanReference) { + sourceBeanName = ((RuntimeBeanReference) source).getBeanName(); + } + else { + parserContext.getReaderContext().error("Wrong 'source' type: must be 'BeanDefinition' or 'RuntimeBeanReference'", source); + } adapterBuilder.addPropertyReference("source", sourceBeanName); adapterBuilder.addPropertyReference("outputChannel", channelName); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml index 933963f608..7a275d23a7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests-context.xml @@ -65,4 +65,12 @@ + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java index 0d96ea5e01..9411fa95f0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/ChannelAdapterParserTests.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -37,6 +38,7 @@ import org.springframework.integration.MessageChannel; import org.springframework.integration.MessageDeliveryException; import org.springframework.integration.MessageDispatchingException; import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.core.MessageSource; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; @@ -294,8 +296,22 @@ public class ChannelAdapterParserTests { } } + @Test + public void testMessageSourceRef() { + PollableChannel channel = this.applicationContext.getBean("messageSourceRefChannel", PollableChannel.class); + + Message message = channel.receive(5000); + assertNotNull(message); + assertEquals("test", message.getPayload()); + + MessageSource testMessageSource = this.applicationContext.getBean("testMessageSource", MessageSource.class); + SourcePollingChannelAdapter adapterWithMessageSourceRef = this.applicationContext.getBean("adapterWithMessageSourceRef", SourcePollingChannelAdapter.class); + MessageSource source = TestUtils.getPropertyValue(adapterWithMessageSourceRef, "source", MessageSource.class); + assertSame(testMessageSource, source); + } + public static class SampleBean { - private String message = "hello"; + private final String message = "hello"; String getMessage() { return message;