/*
 * Copyright 2002-2020 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.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.event;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.AliasFor;

An EventListener that is invoked according to a TransactionPhase. This is an an annotation-based equivalent of TransactionalApplicationListener.

If the event is not published within an active transaction, the event is discarded unless the fallbackExecution flag is explicitly set. If a transaction is running, the event is processed according to its TransactionPhase.

Adding @Order to your annotated method allows you to prioritize that listener amongst other listeners running before or after transaction completion.

NOTE: Transactional event listeners only work with thread-bound transactions managed by PlatformTransactionManager. A reactive transaction managed by ReactiveTransactionManager uses the Reactor context instead of thread-local attributes, so from the perspective of an event listener, there is no compatible active transaction that it can participate in.

Author:Stephane Nicoll, Sam Brannen, Oliver Drotbohm
See Also:
Since:4.2
/** * An {@link EventListener} that is invoked according to a {@link TransactionPhase}. * This is an an annotation-based equivalent of {@link TransactionalApplicationListener}. * * <p>If the event is not published within an active transaction, the event is discarded * unless the {@link #fallbackExecution} flag is explicitly set. If a transaction is * running, the event is processed according to its {@code TransactionPhase}. * * <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated * method allows you to prioritize that listener amongst other listeners running before * or after transaction completion. * * <p><b>NOTE: Transactional event listeners only work with thread-bound transactions * managed by {@link org.springframework.transaction.PlatformTransactionManager}.</b> * A reactive transaction managed by {@link org.springframework.transaction.ReactiveTransactionManager} * uses the Reactor context instead of thread-local attributes, so from the perspective of * an event listener, there is no compatible active transaction that it can participate in. * * @author Stephane Nicoll * @author Sam Brannen * @author Oliver Drotbohm * @since 4.2 * @see TransactionalApplicationListener * @see TransactionalApplicationListenerMethodAdapter */
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @EventListener public @interface TransactionalEventListener {
Phase to bind the handling of an event to.

The default phase is TransactionPhase.AFTER_COMMIT.

If no transaction is in progress, the event is not processed at all unless fallbackExecution has been enabled explicitly.

/** * Phase to bind the handling of an event to. * <p>The default phase is {@link TransactionPhase#AFTER_COMMIT}. * <p>If no transaction is in progress, the event is not processed at * all unless {@link #fallbackExecution} has been enabled explicitly. */
TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;
An optional identifier to uniquely reference the listener.
See Also:
Since:5.3
/** * An optional identifier to uniquely reference the listener. * @since 5.3 * @see TransactionalApplicationListener#getListenerId() */
String id() default "";
Whether the event should be processed if no transaction is running.
/** * Whether the event should be processed if no transaction is running. */
boolean fallbackExecution() default false;
Alias for classes.
/** * Alias for {@link #classes}. */
@AliasFor(annotation = EventListener.class, attribute = "classes") Class<?>[] value() default {};
The event classes that this listener handles.

If this attribute is specified with a single value, the annotated method may optionally accept a single parameter. However, if this attribute is specified with multiple values, the annotated method must not declare any parameters.

/** * The event classes that this listener handles. * <p>If this attribute is specified with a single value, the annotated * method may optionally accept a single parameter. However, if this * attribute is specified with multiple values, the annotated method * must <em>not</em> declare any parameters. */
@AliasFor(annotation = EventListener.class, attribute = "classes") Class<?>[] classes() default {};
Spring Expression Language (SpEL) attribute used for making the event handling conditional.

The default is "", meaning the event is always handled.

See Also:
  • condition.condition
/** * Spring Expression Language (SpEL) attribute used for making the event * handling conditional. * <p>The default is {@code ""}, meaning the event is always handled. * @see EventListener#condition */
String condition() default ""; }