日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

配置spring事务管理的几种方式(声明式事务)

發(fā)布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 配置spring事务管理的几种方式(声明式事务) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

??? DataSource、TransactionManager這兩部分只是會根據(jù)數(shù)據(jù)訪問方式有所變化,比如使用Hibernate進(jìn)行數(shù)據(jù)訪問時,DataSource實(shí)際為SessionFactory,TransactionManager的實(shí)現(xiàn)為HibernateTransactionManager。

具體如下圖:

?根據(jù)代理機(jī)制的不同,總結(jié)了五種Spring事務(wù)的配置方式,配置文件如下:

?第一種方式:每個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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> <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"> <!-- 配置事務(wù)管理器 --> <property name="transactionManager" ref="transactionManager" /> <property name="target" ref="userDaoTarget" /> <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /><!-- 配置事務(wù)屬性 --> <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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> <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"> <!-- 配置事務(wù)管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事務(wù)屬性 --> <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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> <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" /> <!-- 配置事務(wù)屬性 --> <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標(biāo)簽配置的攔截器
<?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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> <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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> <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();}}

?

總結(jié)

以上是生活随笔為你收集整理的配置spring事务管理的几种方式(声明式事务)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲精品四区 | 日本日韩欧美 | 国产宾馆自拍 | eeuss鲁片一区二区三区在线观看 | 色婷婷777777仙踪林 | 男人添女人下部高潮全视频 | 国产精品美女av | 中文幕无线码中文字蜜桃 | 国产免费视屏 | 最新三级网站 | 国产又粗又猛又爽免费视频 | 插插插91| 91精品人妻一区二区三区果冻 | 亚洲ⅴ国产v天堂a无码二区 | 捆绑裸体绳奴bdsm亚洲 | 最新黄色av网站 | 美女一级片 | 日本免费一区二区三区四区五六区 | 91av手机在线 | 欧美黑人xxx | 天天干天天操天天爽 | 在线爱情大片免费观看大全 | 欧美久久久久久久久中文字幕 | 婷婷色影院 | 欧美福利一区二区 | 国产免费一区二区三区三州老师 | 男男免费视频 | 午夜激情视频在线观看 | av日韩一区二区三区 | 五月天婷婷久久 | 亚洲一二三级 | 永久av免费在线观看 | 国产 欧美 精品 | 99久久香蕉 | 麻豆短视频在线观看 | 每日av更新 | 99精品免费在线观看 | 91视频最新入口 | 国产精品推荐 | 亚洲码无人客一区二区三区 | 国产精品亚洲五月天丁香 | 国产国产乱老熟女视频网站97 | 亚洲爱情岛论坛永久 | 中文字幕在线观看网址 | n0659极腔濑亚美莉在线播放播放 | 91九色偷拍| 欧美久久一区二区三区 | 亚洲午夜视频在线观看 | 无码无套少妇毛多18pxxxx | 超碰99在线 | 久久久国产精品视频 | 久久久久亚洲av无码专区喷水 | 日本寂寞少妇 | 中文字幕91视频 | 亚洲av区无码字幕中文色 | 精品99在线观看 | 色综合免费视频 | 成人午夜久久 | 五月天国产视频 | 亚洲熟妇一区二区 | 免费视频一二三区 | 91大神视频在线播放 | 成人毛片在线观看 | 六月婷婷综合 | 日本一区二区高清视频 | 一级免费av | 天天做天天爱天天做 | 天堂视频在线观看免费 | 日韩tv| 老女人人体欣赏a√s | 久久精品视频网 | 亚洲天堂首页 | 日韩国产成人无码av毛片 | 最新久久| 麻豆av导航 | 91高清无打码 | 国语播放老妇呻吟对白 | 久久性精品 | 欧洲av一区二区 | 天堂va欧美va亚洲va老司机 | 自拍偷在线精品自拍偷无码专区 | 久久网伊人 | 免费一区二区三区四区 | 亚洲 欧美 日韩 在线 | 亚洲九九九九 | 免费在线看黄色片 | 午夜男人的天堂 | 永久免费未满蜜桃 | 久久在线免费 | 蘑菇视频黄色 | 69久人妻无码精品一区 | 亚洲第一看片 | 欧美另类videossexo高潮 | 靠逼视频免费网站 | 女人18岁毛片 | 亚洲国产天堂av | 国产毛片毛片毛片毛片毛片 | 黄色特级视频 | 美日韩免费 |