Spring @Order可以应用于@Transactional吗?

Spring @Order可以应用于@Transactional吗?,spring,spring-aop,spring-transactions,Spring,Spring Aop,Spring Transactions,我有一个用@Transactional和另一个自定义注释@custom注释的方法。此自定义注释围绕一个通知进行包装。操作顺序如下所示: 1.Transactional method gets called 2.Sees @Transactional and @Custom 3.As @Custom is intercepted by around advice, it first executes code before invocation of method 4.invocationCont

我有一个用@Transactional和另一个自定义注释@custom注释的方法。此自定义注释围绕一个通知进行包装。操作顺序如下所示:

1.Transactional method gets called
2.Sees @Transactional and @Custom
3.As @Custom is intercepted by around advice, it first executes code before invocation of method
4.invocationContext.proceed()
5.Transaction gets created
6.Actual method runs
7.Back to around advice and executes code after method invocation
1.Transactional method gets called
2.Sees @Transactional and @Custom
3.Transaction gets created (propagate this transaction to @Custom)
4.As @Custom is intercepted by around advice, it first executes code before invocation of method
5.invocationContext.proceed()
6.Actual method runs
7.Back to around advice and executes code after method invocation
我想在调用通知之前创建事务。如下图所示:

1.Transactional method gets called
2.Sees @Transactional and @Custom
3.As @Custom is intercepted by around advice, it first executes code before invocation of method
4.invocationContext.proceed()
5.Transaction gets created
6.Actual method runs
7.Back to around advice and executes code after method invocation
1.Transactional method gets called
2.Sees @Transactional and @Custom
3.Transaction gets created (propagate this transaction to @Custom)
4.As @Custom is intercepted by around advice, it first executes code before invocation of method
5.invocationContext.proceed()
6.Actual method runs
7.Back to around advice and executes code after method invocation
这样,通知和方法都在同一事务中


我们可以在@Transactional上使用@Order,这样首先创建我的交易,然后执行通知吗?

您可以尝试在@custom注释的顶部添加@transaction(propagation=required)。希望它能工作

是的,在
@配置
类中使用:

@EnableTransactionManagement(order = Ordered.HIGHEST_PRECEDENCE)

或者您需要的任何顺序

我猜
@EnableTransactionManagement(order=Ordered.HIGHEST\u priority)
是全局配置

如果您只想使用此方法
@Transactional
,而不想使用其他方法,那么您可以定义方法
B()
来调用
A()
。这种方式将指出注释的顺序

像这样:

// I think this is not a smart solution. 
// If you have a good idea, please let me know.

@Service
class TestB {
    @Autowired
    private TestA testA;


    @Transactional
    public void b() {
        // !! notice: you should call bean testA that will use spring's AOP
        testA.a();
    }
}

@Service
class TestA {
    @Custom
    public void a() {}
}