TransactionalEventListener
事物监听:事物提交前、提交后、回归、完成;
/** * An {@link EventListener} that is invoked according to a {@link TransactionPhase}. * * <p>If the event is not published within the boundaries of a managed 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. * * @author Stephane Nicoll * @author Sam Brannen * @since 4.2 */ @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @EventListener public @interface TransactionalEventListener { /** * 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; /** * Whether the event should be processed if no transaction is running. */ boolean fallbackExecution() default false; /** * Alias for {@link #classes}. */ @AliasFor(annotation = EventListener.class, attribute = "classes") Class<?>[] value() default {}; /** * 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. * <p>The default is {@code ""}, meaning the event is always handled. * @see EventListener#condition */ String condition() default ""; }
/** * The phase at which a transactional event listener applies. * * @author Stephane Nicoll * @author Juergen Hoeller * @since 4.2 * @see TransactionalEventListener */ public enum TransactionPhase { /** * Fire the event before transaction commit. * @see TransactionSynchronization#beforeCommit(boolean) */ BEFORE_COMMIT, /** * Fire the event after the commit has completed successfully. * <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and * therefore executes in the same after-completion sequence of events, * (and not in {@link TransactionSynchronization#afterCommit()}). * @see TransactionSynchronization#afterCompletion(int) * @see TransactionSynchronization#STATUS_COMMITTED */ AFTER_COMMIT, /** * Fire the event if the transaction has rolled back. * <p>Note: This is a specialization of {@link #AFTER_COMPLETION} and * therefore executes in the same after-completion sequence of events. * @see TransactionSynchronization#afterCompletion(int) * @see TransactionSynchronization#STATUS_ROLLED_BACK */ AFTER_ROLLBACK, /** * Fire the event after the transaction has completed. * <p>For more fine-grained events, use {@link #AFTER_COMMIT} or * {@link #AFTER_ROLLBACK} to intercept transaction commit * or rollback, respectively. * @see TransactionSynchronization#afterCompletion(int) */ AFTER_COMPLETION }