Java-如何让@Transactional注解方法被无注解的方法调用时使事务生效
问题
在springboot项目中,同一个类中,无@Transaction注解的方法调用另一个有@Transaction注解的方法,该如何让事务生效?
解法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TransactionalService {
@Autowired
private ApplicationContext applicationContext;
public void outerMethod() {
// 通过 applicationContext 获取代理对象后调用
TransactionalService proxy = applicationContext.getBean(TransactionalService.class);
proxy.innerMethod();
}
@Transactional
public void innerMethod() {
// 业务逻辑
}
}
标签:调用,import,Transactional,springframework,org,注解,时使 From: https://www.cnblogs.com/anhaoyang/p/how-to-make-the-transactions-annotation-method-takes-e