diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/dsl/IntegrationFlowBeanPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/config/dsl/IntegrationFlowBeanPostProcessor.java index 2216f9ff17..974c467b7f 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/dsl/IntegrationFlowBeanPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/dsl/IntegrationFlowBeanPostProcessor.java @@ -160,11 +160,7 @@ public class IntegrationFlowBeanPostProcessor id = flowNamePrefix + id; } - Collection messageHandlers = - this.beanFactory.getBeansOfType(messageHandler.getClass(), false, false) - .values(); - - if (!messageHandlers.contains(messageHandler)) { + if (noBeanPresentForComponent(messageHandler)) { String handlerBeanName = generateBeanName(messageHandler, flowNamePrefix); registerComponent(messageHandler, handlerBeanName, flowBeanName); @@ -175,8 +171,7 @@ public class IntegrationFlowBeanPostProcessor targetIntegrationComponents.put(endpoint, id); } else { - Collection values = this.beanFactory.getBeansOfType(component.getClass(), false, false).values(); - if (!values.contains(component)) { + if (noBeanPresentForComponent(component)) { if (component instanceof AbstractMessageChannel) { String channelBeanName = ((AbstractMessageChannel) component).getComponentName(); if (channelBeanName == null) { @@ -213,10 +208,7 @@ public class IntegrationFlowBeanPostProcessor if (!CollectionUtils.isEmpty(componentsToRegister)) { componentsToRegister.entrySet() .stream() - .filter(o -> - !this.beanFactory.getBeansOfType(o.getKey().getClass(), false, false) - .values() - .contains(o.getKey())) + .filter(o -> noBeanPresentForComponent(o.getKey())) .forEach(o -> registerComponent(o.getKey(), generateBeanName(o.getKey(), flowNamePrefix, o.getValue(), @@ -236,9 +228,7 @@ public class IntegrationFlowBeanPostProcessor targetIntegrationComponents.put(pollingChannelAdapterFactoryBean, id); MessageSource messageSource = spec.get().getT2(); - if (!this.beanFactory.getBeansOfType(messageSource.getClass(), false, false) - .values() - .contains(messageSource)) { + if (noBeanPresentForComponent(messageSource)) { String messageSourceId = id + ".source"; if (messageSource instanceof NamedComponent && ((NamedComponent) messageSource).getComponentName() != null) { @@ -311,10 +301,7 @@ public class IntegrationFlowBeanPostProcessor componentsToRegister.entrySet() .stream() - .filter(component -> - !this.beanFactory.getBeansOfType(component.getKey().getClass(), false, false) - .values() - .contains(component.getKey())) + .filter(component -> noBeanPresentForComponent(component.getKey())) .forEach(component -> registerComponent(component.getKey(), generateBeanName(component.getKey(), component.getValue()))); @@ -355,6 +342,19 @@ public class IntegrationFlowBeanPostProcessor } } + private boolean noBeanPresentForComponent(Object instance) { + if (instance instanceof NamedComponent) { + String beanName = ((NamedComponent) instance).getComponentName(); + if (beanName != null) { + return !this.beanFactory.containsBean(beanName); + } + } + + Collection beans = this.beanFactory.getBeansOfType(instance.getClass(), false, false).values(); + + return !beans.contains(instance); + } + private void registerComponent(Object component, String beanName) { registerComponent(component, beanName, null); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java b/spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java index 71bd0f8533..5b58ce103c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationComponentSpec.java @@ -63,7 +63,7 @@ public abstract class IntegrationComponentSpec requestMessage) { + return requestMessage; + } + + }; + } + + @Bean + public IntegrationFlow flow1WithPrototypeHandler( + @Qualifier("myHandler") AbstractReplyProducingMessageHandler handler) { + return f -> f.handle(handler, e -> e.id("flow1WithPrototypeHandlerConsumer")); + } + + @Bean + public IntegrationFlow flow2WithPrototypeHandler( + @Qualifier("myHandler") AbstractReplyProducingMessageHandler handler) { + return f -> f.handle(handler, e -> e.id("flow2WithPrototypeHandlerConsumer")); + } + } @Service