Skip to content

Commit 21bf69b

Browse files
artembilangaryrussell
authored andcommitted
INT-4367: Fix MessagingMethodInvHelper for CGLIB
JIRA: https://jira.spring.io/browse/INT-4367 When CGLIB proxy is used for messaging POJO invocation, the `InvocableHandlerMethod` doesn't recognize method parameter annotations and therefore the logic is wrong at runtime or just rejected during method processing * Use `AopUtils.selectInvocableMethod()` to select the proper method to call according the provided proxy type
1 parent 2f450f7 commit 21bf69b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,8 @@ else if (!Modifier.isPublic(method1.getModifiers())) {
702702
}
703703
HandlerMethod handlerMethod1;
704704
try {
705-
method1 = org.springframework.util.ClassUtils.getMostSpecificMethod(method1, targetObject.getClass());
705+
method1 = AopUtils.selectInvocableMethod(method1,
706+
org.springframework.util.ClassUtils.getUserClass(targetObject));
706707
InvocableHandlerMethod invocableHandlerMethod =
707708
this.messageHandlerMethodFactory.createInvocableHandlerMethod(targetObject, method1);
708709
handlerMethod1 = new HandlerMethod(invocableHandlerMethod, this.canProcessMessageList);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Map;
3838
import java.util.Optional;
3939
import java.util.Properties;
40+
import java.util.UUID;
4041
import java.util.concurrent.CountDownLatch;
4142
import java.util.concurrent.atomic.AtomicBoolean;
4243
import java.util.concurrent.atomic.AtomicReference;
@@ -74,6 +75,7 @@
7475
import org.springframework.messaging.MessageHeaders;
7576
import org.springframework.messaging.MessagingException;
7677
import org.springframework.messaging.handler.annotation.Header;
78+
import org.springframework.messaging.handler.annotation.Payload;
7779
import org.springframework.messaging.support.GenericMessage;
7880
import org.springframework.util.StopWatch;
7981

@@ -809,6 +811,42 @@ public void handleMessage(Message<?> message) throws MessagingException {
809811
assertTrue(adviceCalled.get());
810812
}
811813

814+
@Test
815+
public void testProxyAndHeaderAnnotation() {
816+
final AtomicReference<Object> payloadReference = new AtomicReference<>();
817+
final AtomicReference<UUID> idReference = new AtomicReference<>();
818+
819+
class MyHandler {
820+
821+
public void handle(@Header(MessageHeaders.ID) UUID id, @Payload Object payload) {
822+
idReference.set(id);
823+
payloadReference.set(payload);
824+
}
825+
826+
}
827+
828+
MyHandler service = new MyHandler();
829+
830+
final AtomicBoolean adviceCalled = new AtomicBoolean();
831+
ProxyFactory proxyFactory = new ProxyFactory(service);
832+
proxyFactory.addAdvice((MethodInterceptor) i -> {
833+
adviceCalled.set(true);
834+
return i.proceed();
835+
});
836+
service = (MyHandler) proxyFactory.getProxy(getClass().getClassLoader());
837+
838+
839+
GenericMessage<String> testMessage = new GenericMessage<>("foo");
840+
841+
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, "handle");
842+
843+
processor.processMessage(testMessage);
844+
845+
assertEquals(testMessage.getPayload(), payloadReference.get());
846+
assertEquals(testMessage.getHeaders().getId(), idReference.get());
847+
assertTrue(adviceCalled.get());
848+
}
849+
812850
@Test
813851
public void testUseSpelInvoker() throws Exception {
814852
UseSpelInvokerBean bean = new UseSpelInvokerBean();

0 commit comments

Comments
 (0)