Browse Source

Find TransactionalEventListener annotation on target method

Closes gh-31034

(cherry picked from commit 6fc4898a1b)
pull/31598/head
Juergen Hoeller 2 years ago
parent
commit
bb46b31925
  1. 12
      spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java
  2. 30
      spring-tx/src/test/java/org/springframework/transaction/event/TransactionalEventListenerTests.java

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -63,13 +63,13 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
*/ */
public TransactionalApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) { public TransactionalApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
super(beanName, targetClass, method); super(beanName, targetClass, method);
TransactionalEventListener ann = TransactionalEventListener eventAnn =
AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); AnnotatedElementUtils.findMergedAnnotation(getTargetMethod(), TransactionalEventListener.class);
if (ann == null) { if (eventAnn == null) {
throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method); throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method);
} }
this.annotation = ann; this.annotation = eventAnn;
this.transactionPhase = ann.phase(); this.transactionPhase = eventAnn.phase();
} }

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

@ -156,6 +156,17 @@ public class TransactionalEventListenerTests {
getEventCollector().assertNoEventReceived(); getEventCollector().assertNoEventReceived();
} }
@Test
public void afterCommitWithTransactionalComponentListenerWithInterfaceProxy() {
load(TransactionalComponentTestListenerWithInterface.class);
this.transactionTemplate.execute(status -> {
getContext().publishEvent("SKIP");
getEventCollector().assertNoEventReceived();
return null;
});
getEventCollector().assertNoEventReceived();
}
@Test @Test
public void afterRollback() { public void afterRollback() {
load(AfterCompletionExplicitTestListener.class); load(AfterCompletionExplicitTestListener.class);
@ -525,6 +536,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 @Component
static class BeforeCommitTestListener extends BaseTransactionalTestListener { static class BeforeCommitTestListener extends BaseTransactionalTestListener {

Loading…
Cancel
Save