我有一个事务服务类:
@Service
@Transactional(...)
public class MyService() {
public void myFunc() {
// some code
}
}
还有以下方面:
@Aspect
public class MyAspect() {
@AfterReturning(value = "execution(...") // pointcut matching myFunc()'s signature
public void doSomethingAfterMyFunc() {
// some code
}
}
我面临的问题是,在输入 @AfterReturning
建议时,通过执行 myFunc()
创建的事务尚未提交,因此该建议共享同一交易。据我所知,这种行为是可以预料的,但出于我的目的,我需要相反的行为 - 有没有办法在输入建议之前提交 myFunc()
的事务?
提前致谢!
最佳答案
发生这种情况是因为 @Transactional
方面,因此您的代码中有 2 个方面,因此您必须按照说明设置 Order
https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj-advice-ordering
在您的代码中,您可以设置自己的方面 MyAspect
的顺序,如下所示:
@Aspect
@Order(1)
public class MyAspect() {
//your code here
}
通过将其顺序指定为 1,因为规则是:
On the way out from the joinpoint, the advice with highest Order value gets executed first.
@Transactional
方面将在您之前执行,因此 myFunc
将被提交,然后 doSomethingAfterMyFunc
将被执行。
关于java - Spring:如何在输入@AfterReturning建议之前提交事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175185/