事物监听管理

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

}

转载于:https://my.oschina.net/yugj/blog/3016762

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值