Merge branch '6.0.x'

# Conflicts:
#	spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapter.java
This commit is contained in:
Juergen Hoeller 2023-08-12 11:35:24 +02:00
commit c65b0a199e
2 changed files with 36 additions and 6 deletions

View File

@ -47,10 +47,10 @@ import org.springframework.util.Assert;
public class TransactionalApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter public class TransactionalApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter
implements TransactionalApplicationListener<ApplicationEvent> { implements TransactionalApplicationListener<ApplicationEvent> {
private final TransactionalEventListener annotation;
private final TransactionPhase transactionPhase; private final TransactionPhase transactionPhase;
private final boolean fallbackExecution;
private final List<SynchronizationCallback> callbacks = new CopyOnWriteArrayList<>(); private final List<SynchronizationCallback> callbacks = new CopyOnWriteArrayList<>();
@ -63,12 +63,12 @@ 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 eventAnn = TransactionalEventListener eventAnn =
AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); AnnotatedElementUtils.findMergedAnnotation(getTargetMethod(), TransactionalEventListener.class);
if (eventAnn == 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 = eventAnn;
this.transactionPhase = eventAnn.phase(); this.transactionPhase = eventAnn.phase();
this.fallbackExecution = eventAnn.fallbackExecution();
} }
@ -91,8 +91,8 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
logger.debug("Registered transaction synchronization for " + event); logger.debug("Registered transaction synchronization for " + event);
} }
} }
else if (this.annotation.fallbackExecution()) { else if (this.fallbackExecution) {
if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) { if (getTransactionPhase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) {
logger.warn("Processing " + event + " as a fallback execution on AFTER_ROLLBACK phase"); logger.warn("Processing " + event + " as a fallback execution on AFTER_ROLLBACK phase");
} }
processEvent(event); processEvent(event);

View File

@ -158,6 +158,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);
@ -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 @Component
static class BeforeCommitTestListener extends BaseTransactionalTestListener { static class BeforeCommitTestListener extends BaseTransactionalTestListener {