diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java index 4b59f70c41..0ac4138bf3 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java @@ -47,10 +47,10 @@ import org.springframework.util.Assert; public class TransactionalApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter implements TransactionalApplicationListener { - private final TransactionalEventListener annotation; - private final TransactionPhase transactionPhase; + private final boolean fallbackExecution; + private final List callbacks = new CopyOnWriteArrayList<>(); @@ -63,12 +63,12 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi public TransactionalApplicationListenerMethodAdapter(String beanName, Class targetClass, Method method) { super(beanName, targetClass, method); TransactionalEventListener eventAnn = - AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); + AnnotatedElementUtils.findMergedAnnotation(getTargetMethod(), TransactionalEventListener.class); if (eventAnn == null) { throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method); } - this.annotation = eventAnn; this.transactionPhase = eventAnn.phase(); + this.fallbackExecution = eventAnn.fallbackExecution(); } @@ -91,8 +91,8 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi logger.debug("Registered transaction synchronization for " + event); } } - else if (this.annotation.fallbackExecution()) { - if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) { + else if (this.fallbackExecution) { + if (getTransactionPhase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) { logger.warn("Processing " + event + " as a fallback execution on AFTER_ROLLBACK phase"); } processEvent(event); diff --git a/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java b/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java index 725e60cf36..0b1e802aee 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java @@ -158,6 +158,17 @@ public class TransactionalEventListenerTests { getEventCollector().assertNoEventReceived(); } + @Test + public void afterCommitWithTransactionalComponentListenerWithInterfaceProxy() { + load(TransactionalComponentTestListenerWithInterface.class); + this.transactionTemplate.execute(status -> { + getContext().publishEvent("SKIP"); + getEventCollector().assertNoEventReceived(); + return null; + }); + getEventCollector().assertNoEventReceived(); + } + @Test public void afterRollback() { load(AfterCompletionExplicitTestListener.class); @@ -552,6 +563,25 @@ public class TransactionalEventListenerTests { } + interface TransactionalComponentTestInterface { + + void handleAfterCommit(String data); + } + + + @Transactional + @Component + static class TransactionalComponentTestListenerWithInterface extends BaseTransactionalTestListener implements + TransactionalComponentTestInterface { + + @TransactionalEventListener(condition = "!'SKIP'.equals(#data)") + @Override + public void handleAfterCommit(String data) { + handleEvent(EventCollector.AFTER_COMMIT, data); + } + } + + @Component static class BeforeCommitTestListener extends BaseTransactionalTestListener {