001/*002 * Copyright 2002-2020 the original author or authors.003 *004 * Licensed under the Apache License, Version 2.0 (the "License");005 * you may not use this file except in compliance with the License.006 * You may obtain a copy of the License at007 *008 * https://www.apache.org/licenses/LICENSE-2.0009 *010 * Unless required by applicable law or agreed to in writing, software011 * distributed under the License is distributed on an "AS IS" BASIS,012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.013 * See the License for the specific language governing permissions and014 * limitations under the License.015 */016017package org.springframework.transaction.event;018019import java.lang.annotation.Documented;020import java.lang.annotation.ElementType;021import java.lang.annotation.Retention;022import java.lang.annotation.RetentionPolicy;023import java.lang.annotation.Target;024025import org.springframework.context.event.EventListener;026import org.springframework.core.annotation.AliasFor;027028/**029 * An {@link EventListener} that is invoked according to a {@link TransactionPhase}.030 *031 * <p>If the event is not published within an active transaction, the event is discarded032 * unless the {@link #fallbackExecution} flag is explicitly set. If a transaction is033 * running, the event is processed according to its {@code TransactionPhase}.034 *035 * <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated036 * method allows you to prioritize that listener amongst other listeners running before037 * or after transaction completion.038 *039 * <p><b>NOTE: Transactional event listeners only work with thread-bound transactions040 * managed by {@link org.springframework.transaction.PlatformTransactionManager}.</b>041 * A reactive transaction managed by {@link org.springframework.transaction.ReactiveTransactionManager}042 * uses the Reactor context instead of thread-local attributes, so from the perspective of043 * an event listener, there is no compatible active transaction that it can participate in.044 *045 * @author Stephane Nicoll046 * @author Sam Brannen047 * @since 4.2048 */049@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})050@Retention(RetentionPolicy.RUNTIME)051@Documented052@EventListener053public @interface TransactionalEventListener {054055 /**056 * Phase to bind the handling of an event to.057 * <p>The default phase is {@link TransactionPhase#AFTER_COMMIT}.058 * <p>If no transaction is in progress, the event is not processed at059 * all unless {@link #fallbackExecution} has been enabled explicitly.060 */061 TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;062063 /**064 * Whether the event should be processed if no transaction is running.065 */066 boolean fallbackExecution() default false;067068 /**069 * Alias for {@link #classes}.070 */071 @AliasFor(annotation = EventListener.class, attribute = "classes")072 Class<?>[] value() default {};073