第一个spring声明式事务管理的例子

来源:互联网 发布:软文发布系统源码 编辑:程序博客网 时间:2024/05/17 23:33

在applicationContext.xml中需要有一些基本配置

<!-- 引入外部的属性文件 -->

    <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.imooc.spring.demol.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置Dao层的类 ,需要将数据库连接池注入到Dao-->
    <bean id="accountDao" class="cn.imooc.spring.demol.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


<!-- AOP -->
    <!-- 配置事务管理器   不管是哪种声明式事务都需要有事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 第一种声明式的事务管理(少用):配置业务层的代理    代理使得aop可以使用IOC来管理切入点和通知-->

    <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!-- 代理的目标对象 -->
        <property name="target" ref="accountService"></property>
        <!-- 注入事务管理器 -->
        <property name="transactionManager" ref="transactionManager"></property>
        <!-- 注入事务的一些属性 -->
        <property name="transactionAttributes">
        <!-- prop的格式 key的值为要用的方法:eg:update*,以update开头的所有方法等,根据自己的需要定义方法
        
            * PROPAGATION  事务的传播行为
            *ISOLATION   :事务的隔离级别
            *readOnly    :只读(不可以进行修改,删除等操作,会出现异常)
            *-Exception  :发生哪些异常回滚
            *+Exception  :发生哪些异常不需要回滚
         -->
            <props>
                <prop key="transfer">[PROPAGATION_REQUIRED,readOnly   ,SERIALIZABLE,+java.Exception.....]</prop>        
            </props>
        </property>
    </bean>

 <!-- 第二种声明式的事务管理:(基于AspectJ)配置事务的通知(事务的通知) -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED" read-only="false" isolation="DEFAULT" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
           <aop:pointcut expression="execution(* cn.imooc.spring.demol3.AccountService+.*(..))" id="pointcut1"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1" />
    </aop:config>


 <!--第三种声明式事务管理:开启一个注解事务    需要在需要使用代理的类上都加上

      @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT......)

-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

/*
 * Dao的实现类
 */

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    /*
     * 转出账户的操作
     * out:转出的账号
     * money:转出的金额
     */
    @Override
    public void outMoney(String out, int money) {
        String sql="update account set money=money - ? where name=?";
        this.getJdbcTemplate().update(sql,money,out);
    }

}


第一种的测试类对于service类的注入要注入的是代理类

      @Resource(name="accountServiceProxy")

       private AccountService accountService;

0 0
原创粉丝点击