Skip to content

Commit 802fb56

Browse files
committed
INT-4571 Made MessageHandlerMethodFactory injectable
Made MessageHandlerMethodFactory injectable into MessagingMethodInvokerHelper Deprecated HandlerMethodArgumentResolversHolder Added 'integrationMessageHandlerMethodFactory' property to IntegrationContextUtils
1 parent e30741f commit 802fb56

File tree

4 files changed

+79
-35
lines changed

4 files changed

+79
-35
lines changed

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
* @author Josh Long
3737
* @author Artem Bilan
3838
* @author Gary Russell
39+
* @author Oleg Zhurakousky
3940
*/
4041
public abstract class IntegrationContextUtils {
4142

@@ -105,6 +106,8 @@ public abstract class IntegrationContextUtils {
105106

106107
public static final String DISPOSABLES_BEAN_NAME = "integrationDisposableAutoCreatedBeans";
107108

109+
public static final String MESSAGE_HANDLER_FACTORY_BEAN_NAME = "integrationMessageHandlerMethodFactory";
110+
108111
/**
109112
* @param beanFactory BeanFactory for lookup, must not be null.
110113
* @return The {@link MetadataStore} bean whose name is "metadataStore".

spring-integration-core/src/main/java/org/springframework/integration/handler/support/HandlerMethodArgumentResolversHolder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,8 +28,9 @@
2828
* @author Gary Russell
2929
*
3030
* @since 5.0
31-
*
31+
* @deprecated as of 5.1.2. Instead simply configure your own MessageHandlerMethodFactory as a bean.
3232
*/
33+
@Deprecated
3334
public class HandlerMethodArgumentResolversHolder {
3435

3536
private final List<HandlerMethodArgumentResolver> resolvers;

spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import org.springframework.messaging.handler.annotation.Headers;
9595
import org.springframework.messaging.handler.annotation.Payload;
9696
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
97+
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
9798
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
9899
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
99100
import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException;
@@ -167,7 +168,7 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
167168
SPEL_COMPILERS.put(SpelCompilerMode.MIXED, EXPRESSION_PARSER_MIXED);
168169
}
169170

170-
private final DefaultMessageHandlerMethodFactory messageHandlerMethodFactory =
171+
private MessageHandlerMethodFactory messageHandlerMethodFactory =
171172
new DefaultMessageHandlerMethodFactory();
172173

173174
private final Object targetObject;
@@ -295,7 +296,14 @@ public void setUseSpelInvoker(boolean useSpelInvoker) {
295296
@Override
296297
public void setBeanFactory(@NonNull BeanFactory beanFactory) {
297298
super.setBeanFactory(beanFactory);
298-
this.messageHandlerMethodFactory.setBeanFactory(beanFactory);
299+
if (isProvidedMessageHandlerFactoryBean()) {
300+
this.messageHandlerMethodFactory = beanFactory.getBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME,
301+
MessageHandlerMethodFactory.class);
302+
}
303+
else {
304+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setBeanFactory(beanFactory);
305+
}
306+
299307
if (beanFactory instanceof ConfigurableListableBeanFactory) {
300308
BeanExpressionResolver beanExpressionResolver = ((ConfigurableListableBeanFactory) beanFactory)
301309
.getBeanExpressionResolver();
@@ -309,8 +317,8 @@ public void setBeanFactory(@NonNull BeanFactory beanFactory) {
309317
@Override
310318
public void setConversionService(ConversionService conversionService) {
311319
super.setConversionService(conversionService);
312-
if (conversionService != null) {
313-
this.messageHandlerMethodFactory.setConversionService(conversionService);
320+
if (conversionService != null && !isProvidedMessageHandlerFactoryBean()) {
321+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setConversionService(conversionService);
314322
}
315323
}
316324

@@ -403,6 +411,11 @@ private MessagingMethodInvokerHelper(Object targetObject, Class<? extends Annota
403411
this.jsonObjectMapper = mapper;
404412
}
405413

414+
private boolean isProvidedMessageHandlerFactoryBean() {
415+
return getBeanFactory() != null
416+
&& getBeanFactory().containsBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME);
417+
}
418+
406419
private void setDisplayString(Object targetObject, Object targetMethod) {
407420
StringBuilder sb = new StringBuilder(targetObject.getClass().getName());
408421
if (targetMethod instanceof Method) {
@@ -503,37 +516,39 @@ private void initializeHandler(HandlerMethod candidate) {
503516
candidate.initialized = true;
504517
}
505518

519+
@SuppressWarnings("deprecation")
506520
private synchronized void initialize() throws Exception {
507521
if (!this.initialized) {
508-
BeanFactory beanFactory = getBeanFactory();
509-
if (beanFactory != null &&
510-
beanFactory.containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
511-
512-
try {
513-
MessageConverter messageConverter =
514-
beanFactory.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME,
515-
MessageConverter.class);
516-
517-
this.messageHandlerMethodFactory.setMessageConverter(messageConverter);
518-
519-
HandlerMethodArgumentResolversHolder handlerMethodArgumentResolversHolder =
520-
beanFactory.getBean(this.canProcessMessageList
521-
? IntegrationContextUtils.LIST_ARGUMENT_RESOLVERS_BEAN_NAME
522-
: IntegrationContextUtils.ARGUMENT_RESOLVERS_BEAN_NAME,
523-
HandlerMethodArgumentResolversHolder.class);
524-
525-
this.messageHandlerMethodFactory.setCustomArgumentResolvers(
526-
handlerMethodArgumentResolversHolder.getResolvers());
522+
if (!isProvidedMessageHandlerFactoryBean()) {
523+
BeanFactory beanFactory = getBeanFactory();
524+
if (beanFactory != null &&
525+
beanFactory.containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
526+
527+
try {
528+
MessageConverter messageConverter =
529+
beanFactory.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME,
530+
MessageConverter.class);
531+
532+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setMessageConverter(messageConverter);
533+
534+
HandlerMethodArgumentResolversHolder handlerMethodArgumentResolversHolder =
535+
beanFactory.getBean(this.canProcessMessageList
536+
? IntegrationContextUtils.LIST_ARGUMENT_RESOLVERS_BEAN_NAME
537+
: IntegrationContextUtils.ARGUMENT_RESOLVERS_BEAN_NAME,
538+
HandlerMethodArgumentResolversHolder.class);
539+
540+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setCustomArgumentResolvers(
541+
handlerMethodArgumentResolversHolder.getResolvers());
542+
}
543+
catch (NoSuchBeanDefinitionException e) {
544+
configureLocalMessageHandlerFactory();
545+
}
527546
}
528-
catch (NoSuchBeanDefinitionException e) {
547+
else {
529548
configureLocalMessageHandlerFactory();
530549
}
550+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).afterPropertiesSet();
531551
}
532-
else {
533-
configureLocalMessageHandlerFactory();
534-
}
535-
536-
this.messageHandlerMethodFactory.afterPropertiesSet();
537552
prepareEvaluationContext();
538553
this.initialized = true;
539554
}
@@ -551,7 +566,7 @@ private void configureLocalMessageHandlerFactory() {
551566
messageConverter = getBeanFactory()
552567
.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME,
553568
MessageConverter.class);
554-
this.messageHandlerMethodFactory.setMessageConverter(messageConverter);
569+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setMessageConverter(messageConverter);
555570
}
556571
else {
557572
messageConverter = new ConfigurableCompositeMessageConverter();
@@ -579,7 +594,7 @@ private void configureLocalMessageHandlerFactory() {
579594

580595
customArgumentResolvers.add(mapArgumentResolver);
581596

582-
this.messageHandlerMethodFactory.setCustomArgumentResolvers(customArgumentResolvers);
597+
((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setCustomArgumentResolvers(customArgumentResolvers);
583598
}
584599

585600
@SuppressWarnings("unchecked")

spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,6 +62,7 @@
6262
import org.springframework.context.ApplicationContext;
6363
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6464
import org.springframework.context.annotation.Configuration;
65+
import org.springframework.context.support.GenericApplicationContext;
6566
import org.springframework.expression.Expression;
6667
import org.springframework.expression.spel.SpelCompilerMode;
6768
import org.springframework.expression.spel.SpelEvaluationException;
@@ -71,6 +72,7 @@
7172
import org.springframework.integration.annotation.ServiceActivator;
7273
import org.springframework.integration.annotation.UseSpelInvoker;
7374
import org.springframework.integration.config.EnableIntegration;
75+
import org.springframework.integration.context.IntegrationContextUtils;
7476
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
7577
import org.springframework.integration.gateway.RequestReplyExchanger;
7678
import org.springframework.integration.handler.support.MessagingMethodInvokerHelper;
@@ -83,6 +85,8 @@
8385
import org.springframework.messaging.MessagingException;
8486
import org.springframework.messaging.handler.annotation.Header;
8587
import org.springframework.messaging.handler.annotation.Payload;
88+
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
89+
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
8690
import org.springframework.messaging.support.GenericMessage;
8791
import org.springframework.util.StopWatch;
8892

@@ -107,6 +111,27 @@ public class MethodInvokingMessageProcessorTests {
107111
@Rule
108112
public ExpectedException expected = ExpectedException.none();
109113

114+
@Test
115+
public void testHandlerMethodFactoryInjection() throws Exception {
116+
SingleMethodJsonWithSpELMessageWildBean bean = new SingleMethodJsonWithSpELMessageWildBean();
117+
MessagingMethodInvokerHelper<?> helper = new MessagingMethodInvokerHelper<>(bean,
118+
SingleMethodJsonWithSpELMessageWildBean.class.getDeclaredMethod("foo", Message.class), false);
119+
GenericApplicationContext context = new GenericApplicationContext();
120+
DefaultMessageHandlerMethodFactory handlerMethodFactory = new DefaultMessageHandlerMethodFactory();
121+
context.registerBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME,
122+
MessageHandlerMethodFactory.class, () -> handlerMethodFactory);
123+
context.refresh();
124+
helper.setBeanFactory(context);
125+
126+
Object injectedHandlerMethodFactory = TestUtils.getPropertyValue(helper, "messageHandlerMethodFactory");
127+
assertThat(injectedHandlerMethodFactory, equalTo(handlerMethodFactory));
128+
129+
Message<?> message = new GenericMessage<>("baz",
130+
Collections.singletonMap(MessageHeaders.CONTENT_TYPE, "application/json"));
131+
helper.process(message);
132+
assertThat(bean.foo.getPayload(), equalTo("baz"));
133+
}
134+
110135
@Test
111136
public void testHandlerInheritanceMethodImplInSuper() {
112137
class A {

0 commit comments

Comments
 (0)