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 at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * 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 and 014 * limitations under the License. 015 */ 016 017package org.springframework.transaction.event; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.context.event.EventListener; 026import org.springframework.core.annotation.AliasFor; 027 028/** 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 discarded 032 * unless the {@link #fallbackExecution} flag is explicitly set. If a transaction is 033 * running, the event is processed according to its {@code TransactionPhase}. 034 * 035 * <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated 036 * method allows you to prioritize that listener amongst other listeners running before 037 * or after transaction completion. 038 * 039 * <p><b>NOTE: Transactional event listeners only work with thread-bound transactions 040 * 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 of 043 * an event listener, there is no compatible active transaction that it can participate in. 044 * 045 * @author Stephane Nicoll 046 * @author Sam Brannen 047 * @since 4.2 048 */ 049@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) 050@Retention(RetentionPolicy.RUNTIME) 051@Documented 052@EventListener 053public @interface TransactionalEventListener { 054 055 /** 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 at 059 * all unless {@link #fallbackExecution} has been enabled explicitly. 060 */ 061 TransactionPhase phase() default TransactionPhase.AFTER_COMMIT; 062 063 /** 064 * Whether the event should be processed if no transaction is running. 065 */ 066 boolean fallbackExecution() default false; 067 068 /** 069 * Alias for {@link #classes}. 070 */ 071 @AliasFor(annotation = EventListener.class, attribute = "classes") 072 Class<?>[] value() default {}; 073 074 /** 075 * The event classes that this listener handles. 076 * <p>If this attribute is specified with a single value, the annotated 077 * method may optionally accept a single parameter. However, if this 078 * attribute is specified with multiple values, the annotated method 079 * must <em>not</em> declare any parameters. 080 */ 081 @AliasFor(annotation = EventListener.class, attribute = "classes") 082 Class<?>[] classes() default {}; 083 084 /** 085 * Spring Expression Language (SpEL) attribute used for making the event 086 * handling conditional. 087 * <p>The default is {@code ""}, meaning the event is always handled. 088 * @see EventListener#condition 089 */ 090 String condition() default ""; 091 092}