zoukankan      html  css  js  c++  java
  • Spring事务管理4-----声明式事务管理(2)

    声明式事务管理  基于AspectJ的 XML 方式配置

      通过对事务管理器TransactionManager配置通知(增强),然后再配置切点和切面,详细见applicationContext.xml配置文件

    这种事务管理对业务层没有代码修改,在xml配置文件中也简化了设置,在真正开发中经常使用。

    dao层

    /**
     * @author AT
     * 转账dao层
     */
    public interface AccountDao {
        /**
         * 转出钱
         * @param outer
         * @param money
         */
        public void remove(String outer,Double money);
        /**
         * 转入钱
         * @param input
         * @param money
         */
        public void add(String input,Double money);
    }

    dao层实现类

    /**
     * 转账dao层实现
     */
    public class AccountDaoimpl extends JdbcDaoSupport implements AccountDao{
        /**
         * 转出钱
         * @param outer
         * @param money
         */
        @Override
        public void remove(String outer, Double money) {
            String sql = "update account set money = money - ? where name = ?";
            this.getJdbcTemplate().update(sql, money,outer);
        }
        /**
         * 转入钱
         * @param input
         * @param money
         */
        @Override
        public void add(String input, Double money) {
            String sql = "update account set money = money + ? where name = ?";
            this.getJdbcTemplate().update(sql, money,input);
        }
        
    }

    service业务层

    /**
     * @author AT
     * 转账业务接口
     */
    public interface AccountSevice {
        public void transfer(String input,String out,Double money);//消费
    }

    service业务层实现类

    /**
     * @author AT
     *  编程式事务管理
     */
    public class AccountServiceImpl implements AccountSevice {
        
        @Resource(name="accountDao")
        private AccountDao accountDao;
    
        @Override
        public void transfer(final String input, final String out,final  Double money) {
            accountDao.remove(out, money);
    //        int a = 1/0;
            accountDao.add(input, money);
        }
         public void setAccountDao(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
    }

    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/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
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop.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 name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
        
        <!-- dao层和业务的类 -->
        <bean id="accountDao" class="com.sdf.spring02.AccountDaoimpl">
            <property name="dataSource" ref="datasource"></property>
        </bean>
        <bean id="accountService" class="com.sdf.spring02.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"/>
        </bean>
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="datasource"/>
        </bean>
        <!-- 配置事务的通知:(事务的增强) -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
            <!--  
                * propagation        事务传播行为
                * isolation         事务隔离级别
                * read-only             只读
                * rollback-for        发生异常回滚
                * no-rollback-for   发生异常不回滚
                * timeout           超时信息
            -->
                <tx:method name="transfer" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- 配置切面 -->
        <aop:config>
            <!-- 配置切入点 -->
            <aop:pointcut expression="execution(* com.sdf.spring02.*.*(..))" id="pointcut1"/>
            <!-- 配置切面 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
        </aop:config>
    </beans>

    测试

    /**
     * @author AT
     * 测试转账信息     声明式事务管理  基于AspectJ的 XML 方式配置
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext02.xml")
    public class AccountTest {
        @Resource(name="accountService")
        private AccountSevice accountService;
        
        @Test
        public void test01(){
            accountService.transfer("A", "B", 300d);
        }
        public void setAccountService(AccountSevice accountService) {
            this.accountService = accountService;
        }
    }
  • 相关阅读:
    Json对象与Json字符串互转(4种转换方式)
    Web.config配置文件详解
    jQuery BlockUI Plugin Demo 6(Options)
    jQuery BlockUI Plugin Demo 5(Simple Modal Dialog Example)
    jQuery BlockUI Plugin Demo 4(Element Blocking Examples)
    jQuery BlockUI Plugin Demo 3(Page Blocking Examples)
    jQuery BlockUI Plugin Demo 2
    <configSections> 位置引起的错误
    关于jQuery的cookies插件2.2.0版设置过期时间的说明
    jQuery插件—获取URL参数
  • 原文地址:https://www.cnblogs.com/kuoAT/p/7804281.html
Copyright © 2011-2022 走看看