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:
Artem Bilan
2018-08-10 12:36:45 -04:00
committed by Gary Russell
parent e2b398ae10
commit 60db7bc159
3 changed files with 116 additions and 25 deletions

View File

@@ -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;
}

View File

@@ -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;
}