diff --git a/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java b/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java index 5adc4954151..40e0bf66a41 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -36,6 +36,7 @@ * @author Josh Long * @author Artem Bilan * @author Gary Russell + * @author Oleg Zhurakousky */ public abstract class IntegrationContextUtils { @@ -105,6 +106,8 @@ public abstract class IntegrationContextUtils { public static final String DISPOSABLES_BEAN_NAME = "integrationDisposableAutoCreatedBeans"; + public static final String MESSAGE_HANDLER_FACTORY_BEAN_NAME = "integrationMessageHandlerMethodFactory"; + /** * @param beanFactory BeanFactory for lookup, must not be null. * @return The {@link MetadataStore} bean whose name is "metadataStore". diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/support/HandlerMethodArgumentResolversHolder.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/support/HandlerMethodArgumentResolversHolder.java index 242a40a68d4..8f29286dc13 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/support/HandlerMethodArgumentResolversHolder.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/support/HandlerMethodArgumentResolversHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2019 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. @@ -28,8 +28,9 @@ * @author Gary Russell * * @since 5.0 - * + * @deprecated as of 5.1.2. Instead simply configure your own MessageHandlerMethodFactory as a bean. */ +@Deprecated public class HandlerMethodArgumentResolversHolder { private final List resolvers; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java index 24cdfcc1609..451f3bc1713 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java @@ -94,6 +94,7 @@ import org.springframework.messaging.handler.annotation.Headers; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; +import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.handler.invocation.InvocableHandlerMethod; import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException; @@ -167,7 +168,7 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator SPEL_COMPILERS.put(SpelCompilerMode.MIXED, EXPRESSION_PARSER_MIXED); } - private final DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = + private MessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory(); private final Object targetObject; @@ -295,7 +296,14 @@ public void setUseSpelInvoker(boolean useSpelInvoker) { @Override public void setBeanFactory(@NonNull BeanFactory beanFactory) { super.setBeanFactory(beanFactory); - this.messageHandlerMethodFactory.setBeanFactory(beanFactory); + if (isProvidedMessageHandlerFactoryBean()) { + this.messageHandlerMethodFactory = beanFactory.getBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME, + MessageHandlerMethodFactory.class); + } + else { + ((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setBeanFactory(beanFactory); + } + if (beanFactory instanceof ConfigurableListableBeanFactory) { BeanExpressionResolver beanExpressionResolver = ((ConfigurableListableBeanFactory) beanFactory) .getBeanExpressionResolver(); @@ -309,8 +317,8 @@ public void setBeanFactory(@NonNull BeanFactory beanFactory) { @Override public void setConversionService(ConversionService conversionService) { super.setConversionService(conversionService); - if (conversionService != null) { - this.messageHandlerMethodFactory.setConversionService(conversionService); + if (conversionService != null && !isProvidedMessageHandlerFactoryBean()) { + ((DefaultMessageHandlerMethodFactory) this.messageHandlerMethodFactory).setConversionService(conversionService); } } @@ -403,6 +411,11 @@ private MessagingMethodInvokerHelper(Object targetObject, Class helper = new MessagingMethodInvokerHelper<>(bean, + SingleMethodJsonWithSpELMessageWildBean.class.getDeclaredMethod("foo", Message.class), false); + GenericApplicationContext context = new GenericApplicationContext(); + DefaultMessageHandlerMethodFactory handlerMethodFactory = new DefaultMessageHandlerMethodFactory(); + context.registerBean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME, + MessageHandlerMethodFactory.class, () -> handlerMethodFactory); + context.refresh(); + helper.setBeanFactory(context); + + Object injectedHandlerMethodFactory = TestUtils.getPropertyValue(helper, "messageHandlerMethodFactory"); + assertThat(injectedHandlerMethodFactory, equalTo(handlerMethodFactory)); + + Message message = new GenericMessage<>("baz", + Collections.singletonMap(MessageHeaders.CONTENT_TYPE, "application/json")); + helper.process(message); + assertThat(bean.foo.getPayload(), equalTo("baz")); + } + @Test public void testHandlerInheritanceMethodImplInSuper() { class A {