Spring事務配置總結:

Spring事務配置總結:
方法1:
ApplicationContent.xml配置:
Beans的配置

<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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd ">


配置事務管理器

<bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

<aop:config proxy-target-class="true">
  <aop:pointcut id="transactionPointcut"
   expression="execution(* service.impl.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />  
</aop:config>

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*"  propagation="REQUIRED"  rollback-for="Exception"/>
   <tx:method name="del*"  propagation="REQUIRED" />
   <tx:method name="*" rollback-for="Exception"/>
  </tx:attributes>
 </tx:advice>



aop後的屬性 proxy-target-class=”true” 默認爲false 爲jdk動態代理,變爲true後爲CGLIB代理,如果在測試的時候沒有設置這個屬性並且在使用getbean方法的時候直接強制裝換爲實現類而不是接口會報$Proxy4 cannot be cast to的錯,解決的辦法有兩種一是配置屬性爲true,二是強制裝換爲接口,當測試類沒有實現某個接口時默認用CGLIB代理,不會出現$Proxy4的錯,在tx中可以設置對於異常的配置默認rollback-for爲RuntimeException,所以這裏需要設置對於異常的控制。


方法二:
配置事務管理器
 <!-- 事務管理器配置 -->
 

<bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <!-- 使用annotation定義事務 -->
 <tx:annotation-driven transaction-manager="txManager"
  proxy-target-class="true" />

@Transactional(rollbackFor=Exception.class)
public class CustServImpl implements ICustServ {
 private CustInfoDAO custInfoDao;

 public CustInfoDAO getCustInfoDao() {
  return custInfoDao;
 }

 public void setCustInfoDao(CustInfoDAO custInfoDao) {
  this.custInfoDao = custInfoDao;
 }
 

 public void addCust(String name) throws Exception {
  // TODO Auto-generated method stub
  CustInfo cust = new CustInfo();
  cust.setName(name);
  //deleteCust("2");
  custInfoDao.save(cust);
  throw new Exception("custADD測試專用");
  
 }
}

直接在使用該類的最前方配置事務,那麼下面的方法都會有相同的配置,也可以對某個單獨的方法配置不同的事務。


注意要點:
1. 在拋異常的時候如果使用try catch 方法的話會把異常吞掉,會捕捉不到異常
2. proxy-target-class="true",這個屬性默認值爲false,使用JDK動態代理,設置爲true後衛GCLIB代理,此時可以使用BlogServImpl blogServ = (BlogServImpl) appContext.getBean("BlogServImpl"); 否則不能,只可以使用接口類型強制裝換IBlogServ iblog = (IBlogServ) appContext.getBean("BlogServImpl");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章