---
title: "spring下iBATIS事务管理-使用AOP XML配置管理【转】"
author: "cheneyph"
date: "2010-12-21T11:01:14Z"
tags: []
categories: ["企业架构"]
slug: "spring-ibatis-aop-xml"
draft: false
lastmod: "2010-12-21T11:01:14Z"
id: 847885
url: "https://cheneyph.iteye.com/blog/847885"
---

Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种，这里记录下述其中的AOP XML配置管理，这也是spring最推荐的方式。

  参照&lt;spring高级程序设计&gt;中的银行转账的例子。

1.Spring的数据源设置

Xml代码 

1. &lt; bean   id = "dataSource"   class = "org.apache.commons.dbcp.BasicDataSource"   destroy-method = "close"   &gt;
2. &lt; property   name = "driverClassName"   value = "com.mysql.jdbc.Driver" /&gt;
3. &lt; property   name = "url"   value = "jdbc:mysql://localhost:3306/test" /&gt;
4. &lt; property   name = "username"   value = "root" /&gt;
5. &lt; property   name = "password"   value = "123456" /&gt;
6. &lt;/ bean &gt;

```
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
  </bean>
```

 2.Spring对iBATIS的支持

   Spring对ibatis主要提供org.springframework.orm.ibatis.SqlMapClientFactoryBean类来进行支持  

Xml代码 

1. &lt; bean   id = "sqlMapClient"   class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" &gt;
2. &lt; property   name = "dataSource"   ref = "dataSource" /&gt;
3. &lt; property   name = "configLocation"   value = "/config/sqlMapConfig.xml" /&gt;
4. &lt;/ bean &gt;

```
  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="/config/sqlMapConfig.xml"/>
  </bean>
```

  3.Spring对iBATIS DAO的支持

  Spring提供org.springframework.orm.ibatis.support.SqlMapClientDaoSupport来对 iBATIS DAO进行支持，通过调用该类的getSqlMapClientTemplate()方法来获得对iBATIS的控制访问。

Xml代码 

1. &lt; bean   id = "accountDao"   class = "com.hj.dao.AccountDaoImp" &gt;
2. &lt; property   name = "sqlMapClient"   ref = "sqlMapClient" /&gt;
3. &lt;/ bean &gt;

```
  <bean id="accountDao" class="com.hj.dao.AccountDaoImp">
     <property name="sqlMapClient" ref="sqlMapClient"/>
  </bean>   
```

 

Xml代码 

1. &lt; bean   id = "bankService"   class = "com.hj.bankOps.DefaultBankService" &gt;
2. &lt; property   name = "accountDao"   ref = "bankAccountDao" /&gt;
3. &lt;/ bean &gt;

```
  <bean id="bankService" class="com.hj.bankOps.DefaultBankService">
     <property name="accountDao" ref="bankAccountDao"/>
  </bean>
```

   这里DefaultBankService类主要实现BankService接口(提供服务的方法定义)，其内部引用一个BankAccountDao实例来对数据库进行访问。BankAccountDao类主要继承SqlMapClientDaoSupport。

 4.Spring 配置事务  

Xml代码 

1. &lt; bean   id = "transactionManager"   class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" &gt;
2. &lt; property   name = "dataSource"   ref = "dataSource" /&gt;
3. &lt;/ bean &gt;

```
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
  </bean> 
```

  5.AOP XML配置事务管理

    1).配置事务通知

Xml代码 

1. &lt; tx:advice   id = "transactionManagerAdivice"   transaction-manager = "transactionManager" &gt;
2. &lt; tx:attributes &gt;
3. &lt; tx:method   name = "\*"
4. isolation = "READ\_COMMITTED"
5. propagation = "REQUIRED"
6. rollback-for = "java.lang.RuntionException"   /&gt;
7. &lt;/ tx:attributes &gt;
8. &lt;/ tx:advice &gt;

```
  <tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager">
     <tx:attributes>
        <tx:method name="*" 
                  isolation="READ_COMMITTED" 
                  propagation="REQUIRED" 
                  rollback-for="java.lang.RuntionException" />
     </tx:attributes>
  </tx:advice>
```

   2).配置切入点和方面

Xml代码 

1. &lt; aop:config &gt;
2. &lt; aop:pointcut   expression = "execution(* com.hj.bankOps.DefaultBankService.\*(..))"   id = "bankServicePc" /&gt;
3. &lt; aop:advisor   advice-ref = "transactionManagerAdivice"   pointcut-ref = "bankServicePc" /&gt;
4. &lt;/ aop:config &gt;

```
 <aop:config>
    <aop:pointcut expression="execution(* com.hj.bankOps.DefaultBankService.*(..))" id="bankServicePc"/>
    <aop:advisor advice-ref="transactionManagerAdivice" pointcut-ref="bankServicePc"/>  
  </aop:config>
```

  上述execution(* com.hj.bankOps.DefaultBankService.\*(..))表达式表示切入点为该类中的任何方法。所以当 DefaultBankService类中方法调用时就会进行事务管理，并且当抛出RuntimeException时，自动进行回滚操作。

  6.遇到的问题

   在&lt;Spring高级程序设计&gt;一书上，对AOP XML事务配置时，其通知部分并没有设置具体属性（缺少 rollback-for="java.lang.RuntionException"）

Xml代码 

1. &lt; tx:attributes &gt;
2. &lt; tx:method   name = "\*"
3. isolation = "READ\_COMMITTED"
4. propagation = "REQUIRED"
5. /&gt;
6. &lt;/ tx:attributes &gt;

```
  <tx:attributes>
        <tx:method name="*" 
                  isolation="READ_COMMITTED" 
                  propagation="REQUIRED" 
                 />
     </tx:attributes>
```

 这样在DefaultBankService方法调用中如果有异常抛出，事务并不进行相应回滚操作。

 

转：[http://jackandroid.iteye.com/blog/614620](http://jackandroid.iteye.com/blog/614620)