博客
关于我
基于注解的AOP实现事务控制
阅读量:687 次
发布时间:2019-03-17

本文共 3057 字,大约阅读时间需要 10 分钟。

以下是优化后的版本:


Spring框架基于注解开发练习项目

本项目对Spring框架的持久层、业务逻辑层以及事务管理等核心知识点进行实践实现,以下是项目的主要实现及解决方案。


模块设计概述

项目基于Spring框架进行开发,主要模块包括:

  • 持久层(DAO):通过QueryRunner快速实现数据库操作
  • 业务逻辑层(Service):基于持久层进行数据处理
  • 事务管理:通过AOP实现全局事务管理
  • 数据库连接管理:使用 ThreadLocal 绑定连接
  • 配置管理:基于Spring的 bean 配置文件

  • 技术选型说明

  • 持久层选择:使用 Apache Commons DbUtils 的 QueryRunner 为持久层提供简便的数据库操作 API
  • 连接管理:内部使用 ThreadLocal 绑定数据库连接,确保每个线程拥有自己的独立连接
  • 事务管理:基于 AOP 式方法 intercepting 进行事务控制
  • 组件扫描:基于 Spring 的 component scan 机制,自动发现 bean 对象

  • 项目实现

    1. 数据访问接口(DAO)

    AccountDao 接口定义:
    public interface AccountDao {    List
    findAllAccount(); Account findAccountById(Integer id); void saveAccount(Account account); void updateAccount(Account account); void deleteAccount(Integer id); Account findAccountByName(String accountName);}
    AccountDaoImpl 实现:
    @Repository("accountDao")public class AccountDaoImpl implements AccountDao {    @Autowired    private QueryRunner runner;    @Autowired    private ConnectionUtils connectionUtils;    @Override    public List
    findAllAccount() { return runner.query(connectionUtils.getThreadConnection(), "SELECT * FROM account", new BeanListHandler
    (Account.class)); } // 其他方法以此类推}

    2. 业务逻辑层(Service)

    AccountService 接口定义:
    public interface AccountService {    void saveAccount(Account account);    void updateAccount(Account account);    void deleteAccount(Integer id);    void transfer(String sourceName, String targetName, Float money);}
    AccountServiceImpl 实现:
    @Service("accountService")public class AccountServiceImpl implements AccountService {    @Autowired    private AccountDao accountDao;    @Override    public void saveAccount(Account account) {        accountDao.saveAccount(account);    }    // 其他方法以此类推}

    3. 事务管理实现

    项目中使用 AOP 方式实现事务管理,核心逻辑如下:

    @Component("txManager")@Aspectpublic class TransactionManager {    @Autowired    private ConnectionUtils connectionUtils;    @Pointcut("execution(* com.qublog.service.impl.*.*(..))")    public void pt1() {}    // 开启事务    public void beginTransaction() {        connectionUtils.getThreadConnection().setAutoCommit(false);    }    // 提交事务    public void commit() {        try {            connectionUtils.getThreadConnection().commit();        } catch (Exception e) {            e.printStackTrace();        }    }    // 回滚事务    public void rollback() {        try {            connectionUtils.getThreadConnection().rollback();        } catch (Exception e) {            e.printStackTrace();        }    }    // 其他方法以此类推}

    4. 测试支持

    public class AccountServiceTest {    @Autowired    private AccountService as;    @Test    public void testTransfer() {        as.transfer("AAA", "BBB", 100f);    }}

    测试说明

    通过单元测试可以验证交易功能的正确性:

  • 测试转账功能:调用 as.transfer("aaa","bbb",100f) 可以验证是否成功实现账户之间的金额转移功能
  • 业务流程测试:确保所有数据库操作均在事务管理范围内
  • 异常处理测试:验证系统在不同异常场景下的回滚能力

  • 核心配置(bean.xml)


    总结

    该项目通过 Spring 框架完成了一个基于注解的持久层和业务逻辑层的实现,重点解决了事务管理和异常处理问题。代码以接口和实现分离的方式做到依赖 injected,符合 Spring 的对应设计理念。通过实际开发经验总结出了一套解决注解处理问题的方案,值得在后续项目中参考应用。

    转载地址:http://dfchz.baihongyu.com/

    你可能感兴趣的文章
    POJ 2484 A Funny Game(神题!)
    查看>>
    POJ 2486 树形dp
    查看>>
    POJ 2488:A Knight's Journey
    查看>>
    SpringBoot为什么易学难精?
    查看>>
    poj 2545 Hamming Problem
    查看>>
    poj 2723
    查看>>
    poj 2763 Housewife Wind
    查看>>
    Qt笔记——模型/视图MVD 文件目录浏览器软件
    查看>>
    POJ 2892 Tunnel Warfare(树状数组+二分)
    查看>>
    poj 2965 The Pilots Brothers' refrigerator-1
    查看>>
    poj 3026( Borg Maze BFS + Prim)
    查看>>
    POJ 3041 - 最大二分匹配
    查看>>
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>