From 60db7bc1590b69fac6b0dd100aeca080ca30e3e7 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 10 Aug 2018 12:36:45 -0400 Subject: [PATCH] INT-4518: Channel late-binding in Messaging Anns JIRA: https://jira.spring.io/browse/INT-4518 * Fix `BridgeFromAnnotationPostProcessor` do not resolve `outputChannel` for the target `BridgeHandler` * Rework logic in the `AbstractMethodAnnotationPostProcessor` to do not resolve a `MessageHandler` bean from the `@Bean` method when we can just check a method return type. * Check `BeanDefinition` instead of real bean when we consult for conditions **Cherry-pick to 5.0.x** --- ...AbstractMethodAnnotationPostProcessor.java | 35 +++--- .../BridgeFromAnnotationPostProcessor.java | 5 +- .../BridgeFromIntegrationTests.java | 101 ++++++++++++++++++ 3 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/annotation/BridgeFromIntegrationTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java index 401a4cc310..3a20244c37 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java @@ -98,7 +98,7 @@ public abstract class AbstractMethodAnnotationPostProcessor messageHandlerAttributes = new ArrayList(); + protected final List messageHandlerAttributes = new ArrayList<>(); protected final ConfigurableListableBeanFactory beanFactory; @@ -129,23 +129,21 @@ public abstract class AbstractMethodAnnotationPostProcessor annotations) { if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { - try { - resolveTargetBeanFromMethodWithBeanAnnotation(method); - } - catch (NoSuchBeanDefinitionException e) { - if (this.logger.isDebugEnabled()) { - this.logger.debug("Skipping endpoint creation; " - + e.getMessage() - + "; perhaps due to some '@Conditional' annotation."); - } + if (!this.beanFactory.containsBeanDefinition(resolveTargetBeanName(method))) { + this.logger.debug("Skipping endpoint creation; perhaps due to some '@Conditional' annotation."); return null; } } - List adviceChain = extractAdviceChain(beanName, annotations); + boolean handlerExists = false; + if (beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { + handlerExists = MessageHandler.class.isAssignableFrom(method.getReturnType()); + } MessageHandler handler = createHandler(bean, method, annotations); + List adviceChain = extractAdviceChain(beanName, annotations); + if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) { ((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain); } @@ -169,12 +167,6 @@ public abstract class AbstractMethodAnnotationPostProcessor annotations) { BridgeHandler handler = new BridgeHandler(); - Object outputChannel = resolveTargetBeanFromMethodWithBeanAnnotation(method); - Assert.isInstanceOf(MessageChannel.class, outputChannel); - handler.setOutputChannel((MessageChannel) outputChannel); + String outputChannelName = resolveTargetBeanName(method); + handler.setOutputChannelName(outputChannelName); return handler; } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/annotation/BridgeFromIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/annotation/BridgeFromIntegrationTests.java new file mode 100644 index 0000000000..e1e5e0cb04 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/annotation/BridgeFromIntegrationTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.config.annotation; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.integration.annotation.BridgeFrom; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.PollableChannel; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author Artem Bilan + * + * @since 5.0.8 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = BridgeFromIntegrationTests.RootTestConfiguration.class) +public class BridgeFromIntegrationTests { + + @Autowired + private MessageChannel gatewayChannel; + + @Autowired + private PollableChannel outputChannel; + + @Test + public void testBridgeFromConfiguration() { + this.gatewayChannel.send(new GenericMessage<>("world")); + + Message receive = this.outputChannel.receive(10_000); + + assertNotNull(receive); + assertEquals("hello world", receive.getPayload()); + } + + + @Configuration + @EnableIntegration + @ComponentScan( + basePackageClasses = BridgeFromIntegrationTests.class, + useDefaultFilters = false, + includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, + classes = { AnnotatedTestService.class, ScannedTestConfiguration.class })) + public static class RootTestConfiguration { + + } + + @Configuration + public static class ScannedTestConfiguration { + + @Bean + @BridgeFrom("gatewayChannel") + public DirectChannel inputChannel() { + return new DirectChannel(); + } + + + @Bean + public DirectChannel gatewayChannel() { + return new DirectChannel(); + } + + + @Bean + public PollableChannel outputChannel() { + return new QueueChannel(); + } + + } + +}