Spring——基于注解的AOP控制事务

基于注解的AOP控制事务

1.拷贝上一次代码

2.applicationContext.xml

xml 复制代码
<!-- 开启spring对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/> 

3.service

java 复制代码
@Service
@Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 转账
     * @param source
     * @param target
     * @param money
     */
    @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);
    }
}

4.测试

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class ServiceTest {
    @Autowired
    private UserService userService;
    /**
     * 转账业务
     */
    @Test
    public void testUpdate(){
        userService.updateUser("张三丰","宋远桥",1F);
    }
}
  • 事务回滚:
相关推荐
她说可以呀15 分钟前
Spring AI 常用 Advisor
人工智能·python·spring
lang2015092836 分钟前
从 J2EE 到云原生:Spring Framework 的设计哲学与演进之路
spring·云原生·java-ee
风流 少年11 小时前
Spring AI 2.0:Advisor
android·人工智能·spring
砍材农夫11 小时前
spring-ai|Spring‑AI 2.0.0 新特性 + 入门教程
spring boot·spring·spring cloud
lv__pf17 小时前
spring之整合mybatis【TL spring 12】
mysql·spring·mybatis
AI人工智能+电脑小能手17 小时前
大白话说Java设计模式-17-装饰器模式(源码剖析篇)
java·spring·设计模式·装饰器模式·源码分析·io流
counting money1 天前
Spring AI Alibaba 从入门到实战:构建企业级 AI 应用
java·人工智能·spring
城管不管1 天前
MySQL 慢查询完整排查
java·服务器·jvm·数据库·mysql·spring·面试
风流 少年1 天前
Spring AI 2.0:Memory
java·后端·spring
Java成神之路-1 天前
Spring Cloud 微服务契约先行:为什么 Controller 建议要实现 Feign 接口?
spring·spring cloud·微服务