Does Spring @Transactional attribute work on a private method?

后端 未结 8 2158
耶瑟儿~
耶瑟儿~ 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(){
            //...
        }
    
    }
    

提交回复
热议问题