博客
关于我
基于注解的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/

    你可能感兴趣的文章
    Oracle 数据库常用SQL语句(1)
    查看>>
    Oracle 数据库特殊查询总结
    查看>>
    Oracle 数据类型
    查看>>
    oracle 数据迁移 怎么保证 和原表的数据顺序一致_一个比传统数据库快 1001000 倍的数据库,来看一看?...
    查看>>
    oracle 时间函数
    查看>>
    oracle 时间转化函数及常见函数 .
    查看>>
    Oracle 权限(grant、revoke)
    查看>>
    oracle 查询clob
    查看>>
    Oracle 比较 B-tree 和 Bitmap 索引
    查看>>
    Oracle 注意点大全
    查看>>
    UML- 组件图(构件图)
    查看>>
    oracle 用户与锁
    查看>>
    oracle 由32位迁移到64位的问题
    查看>>
    oracle 监听器的工作原理
    查看>>
    oracle 行列转换
    查看>>
    oracle 行转列
    查看>>
    Oracle 表
    查看>>
    oracle 课堂笔记
    查看>>
    Oracle 返回结果集的 存储过程
    查看>>
    Oracle 递归
    查看>>