67,515
社区成员
发帖
与我相关
我的任务
分享
@Component
@Transactional
public interface UserDao extends CrudRepository<User,Integer>{
}
class ServiceAImpl implements IServiceA {
@Transactional(propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = Exception.class)
void test1(){
......
}
@Transactional(propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = Exception.class)
void test2(){
......
}
}
class TestController {
@Autowired
private IServiceA serviceA ;
void test(){
//这里test1和test2上面的注解事务是生效的,因为是走的接口。
serviceA.test1();
serviceA.test2();
}
}
//如若换成另一种结构就不同了。
class ServiceAImpl implements IServiceA {
@Transactional(propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = Exception.class)
void test1(){
test2();
}
@Transactional(propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = Exception.class)
void test2(){
......
}
}
class TestController {
@Autowired
private IServiceA serviceA ;
void test(){
//这里test1上面的注解事务是生效的,因为是走的接口。
//test1实现里面调用test2,则test2上面的注解是不生效的,因为实现类内部调用方法不走动态代理。
serviceA.test1();
}
}
参考文章:https://blog.csdn.net/bntX2jSQfEHy7/article/details/79040349
如若你非要在实现类内部调用也要走动态代理,也是有解决方案的,答案就在我给你的链接里面。