• 转账-AOP基于xml和基于注解


    需求:完成转账操作

    环境配置

    -,创建表

    CREATE DATABASE ee19_spring_day03;
    USE ee19_spring_day03;
    CREATE TABLE account(
      id INT PRIMARY KEY AUTO_INCREMENT,
      username VARCHAR(50),
      money INT
    );
    INSERT INTO account(username,money) VALUES('jack','10000');
    INSERT INTO account(username,money) VALUES('rose','10000');

    二,导入jar包

    l  核心:4+1

    l  aop : 4 (aop联盟、spring aop、aspectj规范、spring aspect)

    l  数据库:2  (jdbc/tx)

    l  驱动:mysql

    l  连接池:c3p0

    dao层

    package com.itheima.dao.impl;
    
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    
        @Override
        public void out(String outer, Integer money) {
            this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);
        }
        @Override
        public void in(String inner, Integer money) {
            this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);
        }
    }

    service层

    package com.itheima.service.impl;
    
    import com.itheima.dao.impl.AccountDao;
    public class AccountServiceImpl implements AccountService {
        private AccountDao accountDao;
        public void setAccountDao(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
        @Override
        public void transfer(String outer, String inner, Integer money) {
            accountDao.out(outer, money);
            accountDao.in(inner, money);
        }
    }

    基于xml的形式如下:

    applicationContext.xml文件,放在src下

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                  http://www.springframework.org/schema/beans/spring-beans.xsd
                                  http://www.springframework.org/schema/aop 
                                  http://www.springframework.org/schema/aop/spring-aop.xsd
                                  http://www.springframework.org/schema/context 
                                  http://www.springframework.org/schema/context/spring-context.xsd
                                  http://www.springframework.org/schema/tx 
                                  http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 1 datasource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day03"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123"></property>
        </bean>
        
        <!-- 2 dao  -->
        <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 3 service -->
        <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"></property>
        </bean>
    
    
        <!-- 4 事务管理 -->
        <!-- 4.1 事务管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 4.2 事务详情(事务通知)  , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
            <tx:attributes> 用于配置事务详情(属性属性)
                <tx:method name=""/> 详情具体配置
                    propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
                    isolation 隔离级别
        -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
            </tx:attributes>
        </tx:advice>
        <!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/>
        </aop:config>
    
    </beans>

    测试类

    package com.itheima;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.itheima.service.impl.AccountService;
    public class TestApp {   
        @Test
        public void demo01(){
            //获得容器
            String xmlPath = "applicationContext.xml";
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
            //获得容器内容,不需要自己new,都是从spring中获得,applicationContext.getBean("accountService")指的是id
            AccountService accountService =  (AccountService) applicationContext.getBean("accountService");
            accountService.transfer("jack", "rose", 1000);
        }
    
    }

    转账完成

     基于注解的形式如下:修改的是applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                  http://www.springframework.org/schema/beans/spring-beans.xsd
                                  http://www.springframework.org/schema/aop 
                                  http://www.springframework.org/schema/aop/spring-aop.xsd
                                  http://www.springframework.org/schema/context 
                                  http://www.springframework.org/schema/context/spring-context.xsd
                                  http://www.springframework.org/schema/tx 
                                  http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 1 datasource -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day03"></property>
            <property name="user" value="root"></property>
            <property name="password" value="123"></property>
        </bean>
        
        <!-- 2 dao  -->
        <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 3 service -->
        <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"></property>
        </bean>
    
    
        <!-- 4 事务管理 -->
        <!-- 4.1 事务管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 4.2 将管理器交予spring 
            * transaction-manager 配置事务管理器
            * proxy-target-class
                true : 底层强制使用cglib 代理
        -->
        <tx:annotation-driven transaction-manager="txManager"/>
    
    
    </beans>

    只修改AccountServiceImpl类,如下:添加注解

    package com.itheima.service.impl;
    
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.itheima.dao.impl.AccountDao;
    
    @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)    //这代表的是添加的注解形式
    public class AccountServiceImpl implements AccountService {
    
        private AccountDao accountDao;
        public void setAccountDao(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
        @Override
        public void transfer(String outer, String inner, Integer money) {
            accountDao.out(outer, money);
            //断电
    //        int i = 1/0;
            accountDao.in(inner, money);
        }
    
    }


    运行之前的测试类,即可完成转账,与aop基于xml的形式只有上述的改变而已

  • 相关阅读:
    zookeeper 分布式锁
    kafka比较好的文章
    HTTP深入浅出 http请求
    火狐浏览器下event对象的兼容处理
    透明度轮播框架封装注意点zIndex值不能忘记
    wamp的phpmyadmin无法访问Forbidden的解决方案
    同一DIV内,两个行内块元素不对齐的解决方案
    tween.js运动曲线
    什么是闭包?闭包的优缺点?
    firefox和IE9不支持对icon font字体的跨域访问
  • 原文地址:https://www.cnblogs.com/zjf-293916/p/7441513.html
Copyright © 2020-2023  润新知