关于spring 事务注解失效
情景: 非事务方法A,调用事务方法B,事务方法B失效
原因: spring的事务传递机制,导致B方法失效。
解决办法: 代理。cglib动态增强。
也可以直接在A方法上加事务,这样会降低效率。
public class StudentServiceImpl implements StudentService, InitializingBean{
/**自身引用*/
private StudentService studentService;
@Override
public void afterPropertiesSet() throws Exception {
this.studentService= BeanContainerFactory.getBeanContainer(
StudentServiceImpl.class.getClassLoader()).getBean(
studentService.class);
}
@Override
public void methodA(){
// 调用事务方法B
studentService.methodB();
}
@Override
@Transactional
public void methodB(){
// ....
}
}
本文探讨了在Spring框架中,非事务方法调用事务方法时出现的事务失效现象及其原因。介绍了通过代理机制(如cglib动态增强)来解决这一问题的方法,并提供了一个具体的实现案例。
2936

被折叠的 条评论
为什么被折叠?



