Does Spring @Transactional attribute work on a private method?

后端 未结 8 2045
耶瑟儿~
耶瑟儿~ 2020-11-22 14:03

If I have a @Transactional -annotation on a private method in a Spring bean, does the annotation have any effect?

If the @Transactional annotation is on

相关标签:
8条回答
  • 2020-11-22 14:47

    If you need to wrap a private method inside a transaction and don't want to use aspectj, you can use TransactionTemplate.

    @Service
    public class MyService {
    
        @Autowired
        private TransactionTemplate transactionTemplate;
    
        private void process(){
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    processInTransaction();
                }
            });
    
        }
    
        private void processInTransaction(){
            //...
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:50

    The answer is no. Please see Spring Reference: Using @Transactional :

    The @Transactional annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class

    0 讨论(0)
提交回复
热议问题