Skip to content

Commit 5a8be5d

Browse files
garyrussellartembilan
authored andcommitted
GH-3004: Fix MMIH argument resolution
Fixes #3004 Change the `DefaultMessageHandlerMethodFactory` beans to prototype scope. See spring-projects/spring-framework#23352
1 parent aaefe51 commit 5a8be5d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.beans.factory.config.BeanDefinition;
3939
import org.springframework.beans.factory.config.BeanDefinitionHolder;
4040
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
41+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4142
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4243
import org.springframework.beans.factory.config.PropertiesFactoryBean;
4344
import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -517,6 +518,8 @@ private void registerMessageHandlerMethodFactory() {
517518
if (!this.beanFactory.containsBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME)) {
518519
BeanDefinitionBuilder messageHandlerMethodFactoryBuilder =
519520
createMessageHandlerMethodFactoryBeanDefinition(false);
521+
// TODO: https://github.com/spring-projects/spring-framework/issues/23352
522+
messageHandlerMethodFactoryBuilder.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
520523
this.registry.registerBeanDefinition(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME,
521524
messageHandlerMethodFactoryBuilder.getBeanDefinition());
522525
}
@@ -526,6 +529,8 @@ private void registerListMessageHandlerMethodFactory() {
526529
if (!this.beanFactory.containsBean(IntegrationContextUtils.LIST_MESSAGE_HANDLER_FACTORY_BEAN_NAME)) {
527530
BeanDefinitionBuilder messageHandlerMethodFactoryBuilder =
528531
createMessageHandlerMethodFactoryBeanDefinition(true);
532+
// TODO: https://github.com/spring-projects/spring-framework/issues/23352
533+
messageHandlerMethodFactoryBuilder.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
529534
this.registry.registerBeanDefinition(IntegrationContextUtils.LIST_MESSAGE_HANDLER_FACTORY_BEAN_NAME,
530535
messageHandlerMethodFactoryBuilder.getBeanDefinition());
531536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.handler.support;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.Collections;
22+
import java.util.Map;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.integration.channel.QueueChannel;
30+
import org.springframework.integration.config.EnableIntegration;
31+
import org.springframework.integration.dsl.IntegrationFlow;
32+
import org.springframework.integration.handler.GenericHandler;
33+
import org.springframework.messaging.Message;
34+
import org.springframework.messaging.MessageHeaders;
35+
import org.springframework.messaging.support.GenericMessage;
36+
import org.springframework.test.annotation.DirtiesContext;
37+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
38+
39+
/**
40+
* @author Gary Russell
41+
* @since 5.2
42+
*
43+
*/
44+
@SpringJUnitConfig
45+
@DirtiesContext
46+
public class MessagingMethodInvocableHelperTests {
47+
48+
@Autowired
49+
private Config config;
50+
51+
@Test
52+
void cachedHandler() {
53+
this.config.sampleFlow().getInputChannel().send(new GenericMessage<>(Collections.singletonMap("key", "value")));
54+
Message<?> received = this.config.queue().receive(0);
55+
assertThat(received).isNotNull();
56+
assertThat(received.getPayload()).isEqualTo("Hello value World!");
57+
}
58+
59+
@Configuration
60+
@EnableIntegration
61+
public static class Config {
62+
63+
@Bean
64+
public IntegrationFlow sampleFlow() {
65+
return f -> f
66+
.handle(new MapHandler())
67+
.handle(new StringHandler())
68+
.channel(queue());
69+
}
70+
71+
@Bean
72+
public QueueChannel queue() {
73+
return new QueueChannel();
74+
}
75+
76+
public static class MapHandler implements GenericHandler<Map<String, String>> {
77+
78+
@Override
79+
public String handle(Map<String, String> mapPayload, MessageHeaders messageHeaders) {
80+
return "Hello " + mapPayload.get("key");
81+
}
82+
}
83+
84+
public static class StringHandler implements GenericHandler<String> {
85+
86+
@Override
87+
public String handle(String stringPayload, MessageHeaders messageHeaders) {
88+
return stringPayload + " World!";
89+
}
90+
}
91+
92+
}
93+
94+
}

0 commit comments

Comments
 (0)