Skip to content

Commit 6fc4898

Browse files
committed
Find TransactionalEventListener annotation on target method
Closes gh-31034
1 parent 0c15be0 commit 6fc4898

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -63,13 +63,13 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
6363
*/
6464
public TransactionalApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
6565
super(beanName, targetClass, method);
66-
TransactionalEventListener ann =
67-
AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class);
68-
if (ann == null) {
66+
TransactionalEventListener eventAnn =
67+
AnnotatedElementUtils.findMergedAnnotation(getTargetMethod(), TransactionalEventListener.class);
68+
if (eventAnn == null) {
6969
throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method);
7070
}
71-
this.annotation = ann;
72-
this.transactionPhase = ann.phase();
71+
this.annotation = eventAnn;
72+
this.transactionPhase = eventAnn.phase();
7373
}
7474

7575

spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ public void afterCommitWithTransactionalComponentListenerProxiedViaDynamicProxy(
156156
getEventCollector().assertNoEventReceived();
157157
}
158158

159+
@Test
160+
public void afterCommitWithTransactionalComponentListenerWithInterfaceProxy() {
161+
load(TransactionalComponentTestListenerWithInterface.class);
162+
this.transactionTemplate.execute(status -> {
163+
getContext().publishEvent("SKIP");
164+
getEventCollector().assertNoEventReceived();
165+
return null;
166+
});
167+
getEventCollector().assertNoEventReceived();
168+
}
169+
159170
@Test
160171
public void afterRollback() {
161172
load(AfterCompletionExplicitTestListener.class);
@@ -525,6 +536,25 @@ public void handleAfterCommit(String data) {
525536
}
526537

527538

539+
interface TransactionalComponentTestInterface {
540+
541+
void handleAfterCommit(String data);
542+
}
543+
544+
545+
@Transactional
546+
@Component
547+
static class TransactionalComponentTestListenerWithInterface extends BaseTransactionalTestListener implements
548+
TransactionalComponentTestInterface {
549+
550+
@TransactionalEventListener(condition = "!'SKIP'.equals(#data)")
551+
@Override
552+
public void handleAfterCommit(String data) {
553+
handleEvent(EventCollector.AFTER_COMMIT, data);
554+
}
555+
}
556+
557+
528558
@Component
529559
static class BeforeCommitTestListener extends BaseTransactionalTestListener {
530560

0 commit comments

Comments
 (0)