日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美人与性动交a欧美精品 日韩免费高清视频 | 男人的天堂a在线 | 国产一区二区三区免费播放 | 丝袜av在线播放 | av在线亚洲天堂 | 校园伸入裙底揉捏1v1h | 国产日韩欧美在线观看 | 国产精品999在线观看 | 黄色一级免费视频 | 国产精品毛片一区二区三区 | 国产成人精品一区二区三区无码熬 | 最新中文字幕在线视频 | 龚玥菲三级露全乳视频 | 成人午夜av在线 | 久久九九国产精品 | 看全色黄大色黄大片女一次牛 | 国产一线二线在线观看 | 中国黄色片视频 | 国产精品mv| 免费久久| 国产精品欧美综合 | 天天曰夜夜曰 | 日本成人黄色 | 亚欧精品在线 | 亚洲情se | 国产精品美女久久久久久久 | 亚洲AV无码久久精品国产一区 | 黄页网站在线播放 | 色屁屁www| 色妻av| 亚洲精品福利网站 | 国产3级在线 | 东方影库av | 91免费国产在线观看 | 国产精品s色 | 中文精品一区二区三区 | 精品久久久久久久久久久久久久久 | 中文字幕精品一区二区精 | 日韩欧美一区二区三区视频 | 亚洲天堂免费在线 | 午夜日韩av | 成年人视频免费在线观看 | 国产美女流白浆 | 国产懂色av | 国产精品麻豆一区二区 | 99re在线观看视频 | 打屁股调教视频 | 黄色免费网站在线看 | 国产第一草草影院 | 麻豆一二三区 | 中文在线字幕免费观看 | 亚洲经典一区 | 在线观看视频一区 | 色婷婷国产精品综合在线观看 | 性人久久久久 | 欧美电影一区 | 这里只有精品免费视频 | 99在线视频精品 | 欧美黄色大全 | 亚洲欧美在线播放 | 久久亚洲综合国产精品99麻豆精品福利 | 日韩网站在线播放 | 国产欧美一区二区精品性色99 | 中文字幕精品一区二 | 成人免费视频大全 | 中文在线中文资源 | 河北彩花中文字幕 | 三级在线国产 | 男男啪啪无遮挡 | 国模人体私拍xvideos | 久久久久久久极品 | 久久99精品国产.久久久久 | 男插女视频在线观看 | 国产毛片99| 亚洲国产精品无码专区 | 在线免费观看国产 | 午夜伦情 | 亚洲最大视频网站 | 夜夜夜影院| 国产福利视频网站 | 一区二区在线视频 | 日韩在线91 | 亚洲成人一区二区 | 亚洲国产成人va在线观看天堂 | 99人妻碰碰碰久久久久禁片 | 伊人久久久久噜噜噜亚洲熟女综合 | 69性影院 | 国产另类专区 | 久久国精品 | 一区二区精品区 | 亚洲精品一区二区三区四区乱码 | 国产a√精品区二区三区四区 | 四虎精品在线播放 | 邻居少妇张开双腿让我爽一夜 | 三级免费观看 | 国产又黄又粗又爽 | 神马影院一区二区 | 中文字幕欧美在线 | 草比网站 |