@AfterReturning方面在切入点方法的同一事务中执行? [英] @AfterReturning aspect executes in same transaction of pointcut method?
问题描述
我需要在执行功能后执行任务.我已经为此使用了Aspect.但我有些困惑.
I have a requirement to do a task after execution of a function. I have used Aspect for this. but I have some confusion.
我在spring服务中有一个函数A().
I have a function A() in a spring service.
@Transactional(readOnly = false,
isolation = Isolation.DEFAULT,
propagation = Propagation.REQUIRED,
rollbackFor = {Exception.class})
void A() {
//Do Something
}
我在此功能上有一个@Afterreturning
方面.
I have an @Afterreturning
aspect on this function.
@AfterReturning(pointcut = "execution(Sevice.saverecord(..)) ")
public void processNotifications(JoinPoint jp) {}
我想知道这方面是使用metod A()事务还是它将自动创建一个新事务.由于我尚未对此方面方法定义任何交易.
I want to know whether this aspect will use the transaction of metod A() or it will create a new transaction automatically. As I have not defined any transaction on this aspect method.
我试图在两种方法中都使用TransactionAspectSupport.currentTransactionStatus().toString()打印交易明细.我得到了两个不同的ID.
I tried to print the transaction detail using TransactionAspectSupport.currentTransactionStatus().toString() in both method.I got two different IDs.
任何人都可以解释一下,这是否意味着有两笔交易,如果是,第二笔交易来自何处?
Can anybody explain Does it mean there are two transactions, If yes where this second transaction came from?
推荐答案
是 @AfterReturning
方面的执行与建议方法的事务相同.
Yes the @AfterReturning
aspect executes in the same transaction as that of the advised method.
The execution sequence while invoking a transactional method is depicted in below diagram (ref - spring docs)
方面(即自定义顾问)可以配置为在交易建议之前或之后运行,默认情况下为之后.
The aspect (i.e. custom advisor) can be configured to run before or after the transaction advice, which by default is after.
有关如何配置定制顾问程序以在交易建议之前使用此处.
For further reference on how to configure the custom advisor to run before the transaction advice make use of the Ordered
as described here.
这篇关于@AfterReturning方面在切入点方法的同一事务中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!