用spring动态注册事务监听器? [英] dynamically register transaction listener with spring?

查看:148
本文介绍了用spring动态注册事务监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个springframework应用程序,我想在其中将事务侦听器添加到当前正在进行的事务中.其动机是触发一个提交后操作,该操作将通知下游系统.我正在使用@Transactional将事务包装在某些服务方法周围-这是我要在其中创建/注册事后事务侦听器的地方.我想做一些类似"的事情.

I have a springframework application in which I would like to add a transaction listener to a transaction which is currently in progress. The motivation is to trigger a post commit action which notifies downstream systems. I am using @Transactional to wrap a transaction around some service method -- which is where I want to create/register the post transaction listener. I want to do something "like" the following.

public class MyService {
 @Transaction
 public void doIt() {
  modifyObjects();

  // something like this
  getTransactionManager().registerPostCommitAction(new 
   TransactionSynchronizationAdapter() {
     public void afterCommit() { 
      notifyDownstream(); 
     }
  });
 }
}

Spring有一个TransactionSynchronization接口和适配器类,这正是我想要的;但是,目前尚不清楚如何在当前事务或事务管理器中动态注册一个.如果可以避免的话,我宁愿不继承JtaTransactionManager.

Spring has a TransactionSynchronization interface and adapter class which seems exactly what I want; however it is not immediately clear how to register one dynamically with either the current transaction, or the transaction manager. I would rather not subclass JtaTransactionManager if I can avoid it.

问:.以前有没有人做过.

Q: Has anyone done this before.

问::注册适配器的最简单方法是什么?

Q: what is the simplest way to register my adapter?

推荐答案

您可以使用

you could use an aspect to match transactional methods aspect in your service to accomplish this:

@Aspect
public class AfterReturningExample {

  @AfterReturning("execution(* com.mypackage.MyService.*(..))")
  public void afterReturning() {
    // ...
  }

}

这篇关于用spring动态注册事务监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆