Skip to content

Commit

Permalink
Find TransactionalEventListener annotation on target method
Browse files Browse the repository at this point in the history
Closes gh-31034

(cherry picked from commit 6fc4898)
  • Loading branch information
jhoeller committed Aug 12, 2023
1 parent 0c27510 commit bb46b31
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -63,13 +63,13 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
*/
public TransactionalApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
super(beanName, targetClass, method);
TransactionalEventListener ann =
AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class);
if (ann == null) {
TransactionalEventListener eventAnn =
AnnotatedElementUtils.findMergedAnnotation(getTargetMethod(), TransactionalEventListener.class);
if (eventAnn == null) {
throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method);
}
this.annotation = ann;
this.transactionPhase = ann.phase();
this.annotation = eventAnn;
this.transactionPhase = eventAnn.phase();
}


Expand Down
Expand Up @@ -156,6 +156,17 @@ public void afterCommitWithTransactionalComponentListenerProxiedViaDynamicProxy(
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);
Expand Down Expand Up @@ -525,6 +536,25 @@ public void handleAfterCommit(String data) {
}


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 {

Expand Down

0 comments on commit bb46b31

Please sign in to comment.