zoukankan      html  css  js  c++  java
  • spring事物管理--声明式(AspectJ)(推荐使用)

    1、表结构及数据

    2、需引入的jar包:

    3、业务层(Service)、持久层(Dao)接口与实现类

    Service接口:

    //转账案例业务层接口
    public interface AccountService {
    
        /**
         * @param out    :转出账号
         * @param in    :转入账号
         * @param money    :转账金额
         */
        public void transfer(String out,String in,Double money);
    }

    Service实现类:

    //转账案例业务层实现类
    public class AccountServiceImpl implements AccountService {
    
        //注入转账的Dao
        @Resource
        private AccountDao accountDao;
    
        /**
         * @param out    :转出账号
         * @param in    :转入账号
         * @param money    :转账金额 
         */
        @Override
        public void transfer( String out,  String in,  Double money) {
                    //把业务操作放入内部类中----在一个事物里面(同成功,同失败)
                    accountDao.outMoney(out, money);
                    int i = 1/0;
                    accountDao.inMoney(in, money);
        }
    }

    Dao接口:

    //转账案例持久层接口    
    public interface AccountDao {
        /**
         * @param out    :转出账号
         * @param money    :转账金额 
         */
        public void outMoney(String out,Double money);
        
        /**
         * @param in    :转入账号
         * @param money    :转账金额
         */
        public void inMoney(String in,Double money);
        
    }

    Dao实现类:

    //转账案例持久层实现类
    public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    
        /**
         * @param out    :转出账号
         * @param money    :转账金额 
         */
        @Override
        public void outMoney(String out, Double money) {
            String sql = "update account set money = money - ? where name = ?";
            this.getJdbcTemplate().update(sql,money,out);
        }
    
        /**
         * @param in    :转入账号
         * @param money    :转账金额
         */
        @Override
        public void inMoney(String in, Double money) {
            String sql = "update account set money = money + ? where name = ?";
            this.getJdbcTemplate().update(sql,money,in);
        }
    }

    4、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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context" 
           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-4.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        
        <!-- 引入外部属性文件 -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <!-- 配置c3p0连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
               
         <!-- 配置业务层类 -->
         <bean id="accountService" class="cn.xl.spring.demo3.AccountServiceImpl">
             <property name="accountDao" ref="accountDao"></property>
         </bean>
         
         <!-- 配置持久层类 -->
         <bean id="accountDao" class="cn.xl.spring.demo3.AccountDaoImpl">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <!-- 事物管理器配置 -->
         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <!-- 配置事物的通知:事物的增强 -->
         <tx:advice id="txAdvice" transaction-manager="transactionManager">
             <tx:attributes>
         <!--
           propagation :事物传播行为

            isolation   :事物隔离级别
            read-only   :只读
            rollback-for :发生哪些异常回滚
            no-rollback-for :发生哪些异常不回滚
            timeout :过期信息
            -->

                 <tx:method name="*" propagation="REQUIRED"/>
             </tx:attributes>
         </tx:advice>
         
         <!-- 配置切面 -->
         <aop:config>
                 <!-- 配置切入点 -->
                 <aop:pointcut expression="execution(* cn.xl.spring.demo3.AccountService+.*(..))" id="pointcut1"/>
                 <!-- 配置切面 -->
                 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
         </aop:config>
    </beans>

    注意:

    (1)、Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 
    (2)、区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcut和多个advice

    (3)、无论使用哪种方式实现spring的事物管理,在applicationContext.xml文件中都必须配置事务管理器,并将连接池设置为管理器的属性

    5、测试类:

    //spring声明式事物管理的方式二的测试类:基于AspectJ的XML配置
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext3.xml")
    public class SpringDemo3 {
        //需注入业务层代理对象
        @Resource(name="accountService")
        private AccountService accountService;
        
        @Test
        public void demo3(){
            accountService.transfer("aaa", "bbb", 200d);
        }
    }
  • 相关阅读:
    系统相关的信息模块: import sys
    Mysql 解决emoji表情处理问题
    Servlet 个人理解
    Java BufferImage 获取像素矩阵 或 数组
    SVN 出现 R6034 错误 Python导致异常
    Mycat 入门
    Mycat 入门
    Nginx 架构
    Nginx 架构 性能优化
    Nginx 架构 性能优化
  • 原文地址:https://www.cnblogs.com/xl118/p/6793337.html
Copyright © 2011-2022 走看看