Spring 事務配置5種方式

?Spring配置文件中關于事務配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。

?DataSource、TransactionManager這兩部分只是會根據數據訪問方式有所變化,比如使用Hibernate進行數據訪問時,DataSource實際為SessionFactory,TransactionManager的實現為HibernateTransactionManager。

?具體如下圖:

?

?根據代理機制的不同,總結了五種Spring事務的配置方式,配置文件如下:

第一種方式:每個Bean都有一個代理

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定義事務管理器(聲明式的事務) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置DAO --><bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="userDao"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  <!-- 配置事務管理器 -->  <property name="transactionManager" ref="transactionManager" />     <property name="target" ref="userDaoTarget" />  <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /><!-- 配置事務屬性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop></props>  </property>  </bean>  
</beans>

第二種方式:所有Bean共享一個代理基類

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定義事務管理器(聲明式的事務) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="transactionBase"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  lazy-init="true" abstract="true">  <!-- 配置事務管理器 -->  <property name="transactionManager" ref="transactionManager" />  <!-- 配置事務屬性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop>  </props>  </property>  </bean>    <!-- 配置DAO --><bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="userDao" parent="transactionBase" >  <property name="target" ref="userDaoTarget" />   </bean>
</beans>

第三種方式:使用攔截器

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定義事務管理器(聲明式的事務) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean> <bean id="transactionInterceptor"  class="org.springframework.transaction.interceptor.TransactionInterceptor">  <property name="transactionManager" ref="transactionManager" />  <!-- 配置事務屬性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop>  </props>  </property>  </bean><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  <property name="beanNames">  <list>  <value>*Dao</value></list>  </property>  <property name="interceptorNames">  <list>  <value>transactionInterceptor</value>  </list>  </property>  </bean>  <!-- 配置DAO --><bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>

第四種方式:使用tx標簽配置的攔截器

<?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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config /><context:component-scan base-package="com.bluesky" /><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定義事務管理器(聲明式的事務) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="interceptorPointCuts"expression="execution(* com.bluesky.spring.dao.*.*(..))" /><aop:advisor advice-ref="txAdvice"pointcut-ref="interceptorPointCuts" />        </aop:config>      
</beans>

第五種方式:全注解

<?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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config /><context:component-scan base-package="com.bluesky" /><tx:annotation-driven transaction-manager="transactionManager"/><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定義事務管理器(聲明式的事務) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean></beans>

此時在DAO上需加上@Transactional注解,如下:

package com.bluesky.spring.dao;import java.util.List;import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;import com.bluesky.spring.domain.User;@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {public List<User> listUsers() {return this.getSession().createQuery("from User").list();}}

?

轉載于:https://www.cnblogs.com/gy19920604/p/5288613.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/257524.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/257524.shtml
英文地址,請注明出處:http://en.pswp.cn/news/257524.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java中主線程首先執行_java經典面試題:子線程先運行30次主線程,主線程40次,如此循環50次?...

最近偶遇這道題&#xff0c;網上相似的題都是循環次數不一樣。然而我百度搜到的論壇或者博客感覺都不太對&#xff0c;運行有穿插。請給出正確結果。我們假使所有人都引入了業務對象。并且我有疑問&#xff1f;感覺題目本意不是new Thread()放在前面。網上有人做法是用標志位防…

[翻譯]Feedback on the Go Challenge solutions

第一次Go Challenge比賽&#xff0c;中國區只有3人參賽。 賽后收到郵件&#xff0c;是一個審閱者的反饋&#xff0c;“Feedback on the Go Challenge solutions”&#xff0c;摘錄如下&#xff1a; 保持簡單粗暴 一個語義單元一個文件即可&#xff0c;不要像Java那樣一個文件就…

黑客宣稱掌握了600多萬個Instagram賬號的信息

據外媒報道&#xff0c;上周早些時候&#xff0c;歌手兼演員賽琳娜戈麥斯因Instagram賬號被盜而發出大量來自前男友賈斯汀比伯的裸照。不過當時很快賽琳娜就拿回了對賬號的控制權并刪掉了這些裸照。就在大家以為這件事情已經平息的時候&#xff0c;Instagram卻被曝光了一個極為…

java apache.poi_Java Apache POI

我正在努力從excel文檔中讀取數據,該文檔每兩周更新一次,大約有50,000行數據,在開始新工作表之前可能會達到大約120,000.我正在使用Apache POI來獲取數據.我在下面得到了這個例外,但我認為最重要的一個例外是引起&#xff1a;java.lang.OutOfMemoryError&#xff1a;Java堆空間…

Hibernate逍遙游記-第2章-使用hibernate.properties

1. 1 package mypack;2 3 import org.hibernate.*;4 import org.hibernate.cfg.Configuration;5 import java.util.*;6 7 public class BusinessService{8 public static SessionFactory sessionFactory;9 10 /** 初始化Hibernate&#xff0c;創建SessionFactory實例 */1…

奇怪吸引子---Aizawa

奇怪吸引子是混沌學的重要組成理論&#xff0c;用于演化過程的終極狀態&#xff0c;具有如下特征&#xff1a;終極性、穩定性、吸引性。吸引子是一個數學概念&#xff0c;描寫運動的收斂類型。它是指這樣的一個集合&#xff0c;當時間趨于無窮大時&#xff0c;在任何一個有界集…

C#打印圖片

打印的原理是&#xff1a;生成mdi文件&#xff0c;系統碰到mdi的時候會自動以打印的方式處理。所以&#xff0c;不管用什么模板&#xff0c;什么方式&#xff1b;能在PrintPage事件處理中,生成一張要打印內容的圖片就OK了! C#實現打印源碼如下&#xff1a; #region 打印 …

mysql 里面不等于符號_mysql 不等于 符號寫法

經過測試發現mysql中用<>與!都是可以的&#xff0c;但sqlserver中不識別!,所以建議用<>selece * from jb51 where id<>45sql 里 符號<> 于 ! 的區別<> 與!都是不等于的意思&#xff0c;但是一般都是用<>來代碼不等于因為<>在任何SQL…

Delphi通過ICMP檢測與遠程主機連接

{ ping IP 地址&#xff08;返回false or true&#xff09; 2015-03-23} function PingHost(HostIP: String): Boolean; typePIPOptionInformation ^TIPOptionInformation;TIPOptionInformation packed recordTTL:Byte;TOS:Byte;Flags:Byte;OptionsSize:Byte;OptionsData:PC…

安裝SQL2012出現[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)設置為 1

本人安裝SQL2012出現這個錯誤&#xff0c;找了三天三夜&#xff0c;終于把問題找出來&#xff0c;共享給有需要的人們&#xff0c;不用重新換系統 錯誤如下: 1&#xff0c;此問題是系統.net Framework版本沖突&#xff0c;首先下載.net Framework清理工具&#xff08;如:cleanu…

Java學習筆記之equals和Objects.equals

equals 相信大家就知道&#xff0c;就是比較&#xff0c;我們平時也會在自己定義的類中加入自己重寫的equals用來比較兩個類是否相同&#xff0c;例如這樣 public class Person {private String name; //姓名private int age; //年齡private String nickName; //昵稱public Per…

java限制發送短信次數_使用java發送短信驗證碼碼,出現流量限制怎么辦?急急急...

注冊登錄后需要企業認證,直接在某度上找一張清晰有紅章的企業營業執照,注意要細心點,要看看有沒有水印。我第一次就沒注意上傳了一張有水印的營業執照&#xff0c;從此這個賬號再也沒有審核通過了&#xff0c;后面只能換個賬號。都是后臺人工審核的&#xff0c;比較嚴格。如果時…

GDKOI2015 Day2

P1 題目描述&#xff1a; 給出一個二分圖&#xff0c;選擇互不相交的邊&#xff0c;使得邊覆蓋的點權和最大。 solution&#xff1a; 簡單DP&#xff0c;用樹狀數組維護最大值。 時間復雜度&#xff1a;$O(n \log n) $ P2 題目描述&#xff1a; 給出N個或黑或白的元素&#xff…

寫在SDOI2016Round1前的To Do List

理性的整理了一下自己的不足。 計算幾何啥都不會&#xff0c;字符串類DP毫無練習&#xff0c;數據結構寫的不熟&#xff0c;數論推不出式子&#xff0c;網絡流建模常建殘&#xff1b; 需要達成的任務&#xff1a; 一、網絡流&#xff1a; 熟練網絡流的板子&#xff08;之前一…

XMind入門教程

最近在總結一些框架知識的時候&#xff0c;總找不到一款好的軟件來畫流程圖&#xff0c;后來在網上查找這方面的東西&#xff0c;找到了 XMind,發現用來畫思維導圖還挺好的&#xff0c;看起來思路清晰&#xff0c;美觀。那么便將使用的一些經驗分享給大家。 1、什么是思維導圖&…

標簽與表格

bgcolor 頁面背景色 text 文字顏色 topmargain 上頁邊距 leftmargain 左頁邊距 rightmargain 右頁邊距 bottomargain 下頁邊距 background 背景壁紙 &nbsp 空…

java word轉圖片tiff_不怕復制內容 Word轉存TIFF文件這么玩

辛辛苦苦把Word文件敲好&#xff0c;為了不讓別人復制走內容&#xff0c;只能看文稿&#xff0c;有些人就選擇轉存成PDF文件——但是PDF文件依然可以被編輯&#xff0c;還有什么方法能防范呢&#xff1f;其實在Word 2003之前&#xff0c;用戶可以通過Microsoft Office Document…

item-設置可見性

如果我們想要設置menu中item的可見行&#xff0c;有兩種方式&#xff1a; 1.直接在menu的xml代碼中設置 <menu> <item android:id"id/action_hotknot"android:showAsAction"always"android:icon"drawable/action_mode_hotknot"android:…

IDC:聚焦6+6,抓住數字化轉型商機

今天&#xff0c;IDC中國2015年中國ICT市場趨勢論壇巡回系列的第二站在北京舉行。論壇的主題為“加速創新實現數字化轉型”。 這是最壞的時代&#xff1a;經濟增長乏力、實體經濟不振、傳統行業在被顛覆與重構、IT市場總體增長進入個位數區間、IT第二平臺的領導廠商仍在困境中。…

編寫EL函數

1.建立java類的靜態函數 package chapter4;public class ELFun {public static String processStr(String s){s s.replaceAll("<", "&lt");s s.replaceAll(">", "&gt");s s.replaceAll(" ", " "…