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**
This commit is contained in:
committed by
Gary Russell
parent
e2b398ae10
commit
60db7bc159
@@ -98,7 +98,7 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
|
||||
|
||||
protected final Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
protected final List<String> messageHandlerAttributes = new ArrayList<String>();
|
||||
protected final List<String> messageHandlerAttributes = new ArrayList<>();
|
||||
|
||||
protected final ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@@ -129,23 +129,21 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
|
||||
@Override
|
||||
public Object postProcess(Object bean, String beanName, Method method, List<Annotation> 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<Advice> 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<Advice> adviceChain = extractAdviceChain(beanName, annotations);
|
||||
|
||||
if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) {
|
||||
((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain);
|
||||
}
|
||||
@@ -169,12 +167,6 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
|
||||
}
|
||||
}
|
||||
|
||||
boolean handlerExists = false;
|
||||
if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
|
||||
Object handlerBean = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
|
||||
handlerExists = handlerBean != null && handler == handlerBean;
|
||||
}
|
||||
|
||||
if (!handlerExists) {
|
||||
String handlerBeanName = generateHandlerBeanName(beanName, method);
|
||||
if (handler instanceof ReplyProducingMessageHandlerWrapper
|
||||
@@ -228,7 +220,9 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
|
||||
if (endpoint != null) {
|
||||
return endpoint;
|
||||
}
|
||||
return handler;
|
||||
else {
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -450,14 +444,11 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation
|
||||
}
|
||||
|
||||
protected String resolveTargetBeanName(Method method) {
|
||||
String id = null;
|
||||
String id = method.getName();
|
||||
String[] names = AnnotationUtils.getAnnotation(method, Bean.class).name();
|
||||
if (!ObjectUtils.isEmpty(names)) {
|
||||
id = names[0];
|
||||
}
|
||||
if (!StringUtils.hasText(id)) {
|
||||
id = method.getName();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,9 +73,8 @@ public class BridgeFromAnnotationPostProcessor extends AbstractMethodAnnotationP
|
||||
@Override
|
||||
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user