zoukankan      html  css  js  c++  java
  • SpringAOP测试

    简单转账功能

    新建Java项目

    SpringAopLieJie;设置Maven版本、配置文件以及仓库

    mysql创建数据

    # 删除spring_aop数据库
    drop database if exists spring_aop;
    # 创建spring_aop数据库
    create database spring_aop;
    # 使用spring_aop数据库
    use spring_aop;
    # 创建account表
    create table account (
    id int(11) auto_increment primary key,
    accountNum varchar(20) default NULL,
    money int(8) default 0
    );
    # 新增数据
    insert into account (accountNum, money) values
    ("622200001",1000),("622200002",1000);

    导包

    Spring连接Mysql

    项目代码结构:

    Utils——数据库连接工具类:ConnectionUtils.java

    entity——实体类:Account.java

    Dao层:AccountDao.java

    Dao层实现类:AccountDaoImpl.java

    Service层:AccountService.java

    Service层实现类:AccountServiceImpl.java

    Test——测试类:AccountTest.java

    测试结果:

    mysql结果图:

    报异常——用事务处理,开启事务,提交、回滚等

    引入代理模式解决事务

    事务管理器:TransactionManager.java

    事务代理工具类:TransactionProxyUtils

    引入AOP

    代码:

    1.删除事务代理工具类:TransactionProxyUtils

    2.导包

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.3</version>
    </dependency>

    3.添加AOP配置——

    <!-- aop相关的节点配置 -->
    <aop:config>
    <!-- 切入点 表示哪些类的哪些方法在执行的时候会应用Spring配置的通知进行增强 -->
    <aop:pointcut expression="execution ( * services.*.*(..))" id="pc"/>
    <!-- 配置切面类的节点 作用主要就是整合通知和切入点 -->
    <aop:aspect ref="transactionManager">
    <aop:before method="beginTransaction" pointcut-ref="pc"/>
    <aop:after-returning method="commit" pointcut-ref="pc"/>
    <aop:after method="release" pointcut-ref="pc"/>
    <aop:after-throwing method="rollback" pointcut-ref="pc"/>
    </aop:aspect>
    </aop:config>
  • 相关阅读:
    Express ejs 3.* layout.ejs
    old header
    mac 命令行 安装 需要管理员 权限
    Understanding the Debug Log
    insufficient_access_on_cross_reference_entity APEX / Salesforce
    custom list view
    exam help
    Backbone.js Wine Cellar 教程
    理解RESTful架构
    SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
  • 原文地址:https://www.cnblogs.com/Anm214/p/14586704.html
Copyright © 2011-2022 走看看