From b2d7867ce258a6fcd7c3cbbe72515135ec6e0884 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 22 Oct 2020 12:03:06 -0400 Subject: [PATCH] Fix `@InChAdapter` for several supplier beans The `InboundChannelAdapterAnnotationPostProcessor` doesn't use a bean method name when it parses a `Supplier` bean and only uses a configuration class name + `get` for method part * Fix `InboundChannelAdapterAnnotationPostProcessor` to also include a bean method name into the final bean name for the `MethodInvokingMessageSource` based on the `Supplier` bean * Modify `ReactiveInboundChannelAdapterTests` to add one more `Supplier` with the `@InboundChannelAdapter` to ensure that configuration is still valid after the fix **Cherry-pick to 5.2.x & 5.1.x** --- .../InboundChannelAdapterAnnotationPostProcessor.java | 11 +++++++---- .../endpoint/ReactiveInboundChannelAdapterTests.java | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/InboundChannelAdapterAnnotationPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/InboundChannelAdapterAnnotationPostProcessor.java index e91f0eb8c8..5385fb1484 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/InboundChannelAdapterAnnotationPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/annotation/InboundChannelAdapterAnnotationPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -79,7 +79,7 @@ public class InboundChannelAdapterAnnotationPostProcessor extends .resolveAttribute(annotations, AnnotationUtils.VALUE, String.class); Assert.hasText(channelName, "The channel ('value' attribute of @InboundChannelAdapter) can't be empty."); - MessageSource messageSource = null; + MessageSource messageSource; try { messageSource = createMessageSource(bean, beanName, method); } @@ -100,10 +100,11 @@ public class InboundChannelAdapterAnnotationPostProcessor extends return adapter; } - private MessageSource createMessageSource(Object beanArg, String beanName, Method methodArg) { + private MessageSource createMessageSource(Object beanArg, String beanNameArg, Method methodArg) { MessageSource messageSource = null; Object bean = beanArg; Method method = methodArg; + String beanName = beanNameArg; if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method); Class targetClass = target.getClass(); @@ -119,10 +120,12 @@ public class InboundChannelAdapterAnnotationPostProcessor extends else if (target instanceof Supplier) { method = ReflectionUtils.findMethod(Supplier.class, "get"); bean = target; + beanName += '.' + methodArg.getName(); } else if (kotlinFunction0Class != null) { method = ReflectionUtils.findMethod(kotlinFunction0Class, "invoke"); bean = target; + beanName += '.' + methodArg.getName(); } } if (messageSource == null) { @@ -143,7 +146,7 @@ public class InboundChannelAdapterAnnotationPostProcessor extends @Override protected String generateHandlerBeanName(String originalBeanName, Method method) { return super.generateHandlerBeanName(originalBeanName, method) - .replaceFirst(IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX + "$", ".source"); + .replaceFirst(IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX + '$', ".source"); } @Override diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReactiveInboundChannelAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReactiveInboundChannelAdapterTests.java index 6e50f8c759..5b2065de33 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReactiveInboundChannelAdapterTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ReactiveInboundChannelAdapterTests.java @@ -87,6 +87,12 @@ public class ReactiveInboundChannelAdapterTests { }; } + @Bean + @InboundChannelAdapter(value = "fluxChannel", autoStartup = "false", poller = @Poller(fixedDelay = "100000")) + public Supplier anotherSupplier() { + return () -> "void"; + } + @Bean public MessageChannel fluxChannel() { return new FluxMessageChannel();