Avoid transaction listener execution without transaction management setup
Closes gh-32319
This commit is contained in:
@@ -91,6 +91,8 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||
@Nullable
|
||||
private final String condition;
|
||||
|
||||
private final boolean defaultExecution;
|
||||
|
||||
private final int order;
|
||||
|
||||
@Nullable
|
||||
@@ -119,6 +121,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||
EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class);
|
||||
this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
|
||||
this.condition = (ann != null ? ann.condition() : null);
|
||||
this.defaultExecution = (ann == null || ann.defaultExecution());
|
||||
this.order = resolveOrder(this.targetMethod);
|
||||
String id = (ann != null ? ann.id() : "");
|
||||
this.listenerId = (!id.isEmpty() ? id : null);
|
||||
@@ -166,7 +169,9 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
processEvent(event);
|
||||
if (isDefaultExecution()) {
|
||||
processEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -227,6 +232,16 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||
return ClassUtils.getQualifiedMethodName(method) + sj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether default execution is applicable for the target listener.
|
||||
* @since 6.2
|
||||
* @see #onApplicationEvent
|
||||
* @see EventListener#defaultExecution()
|
||||
*/
|
||||
protected boolean isDefaultExecution() {
|
||||
return this.defaultExecution;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process the specified {@link ApplicationEvent}, checking if the condition
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -132,6 +132,14 @@ public @interface EventListener {
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
/**
|
||||
* Whether the event should be handled by default, without any special
|
||||
* pre-conditions such as an active transaction. Declared here for overriding
|
||||
* in composed annotations such as {@code TransactionalEventListener}.
|
||||
* @since 6.2
|
||||
*/
|
||||
boolean defaultExecution() default true;
|
||||
|
||||
/**
|
||||
* An optional identifier for the listener, defaulting to the fully-qualified
|
||||
* signature of the declaring method (e.g. "mypackage.MyClass.myMethod()").
|
||||
|
||||
Reference in New Issue
Block a user