8.基于注解的AOP控制事务
8.1.拷贝上一章代码
8.2.applicationContext.xml
<tx:annotation-driven transaction-manager="transactionManager"/>
8.3.service
@Service
@Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void updateUser(String source, String target, Float money) {
userMapper.updateUserOfSub(source, money);
int a = 6/0;
userMapper.updateUserOfAdd(target, money);
}
}
8.4.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ServiceTest {
@Autowired
private UserService userService;
@Test
public void testUpdate(){
userService.updateUser("张三丰","宋远桥",1F);
}
}