最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring配置事務(wù)的五種方式

 更新時(shí)間:2016年06月15日 14:22:59   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Spring配置事務(wù)的五種方式,感興趣的小伙伴們可以參考一下

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

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

具體如下圖:

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

第一種方式:每個(gè)Bean都有一個(gè)代理

<?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.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
 
http://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共享一個(gè)代理基類

<?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.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
 
http://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.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
 
http://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.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://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.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://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>

此時(shí)在DAO上需加上@Transactional注解,如下:

package com.mktao.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(); 
 } 
... 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解SpringBoot封裝使用JDBC

    詳解SpringBoot封裝使用JDBC

    這篇文章主要介紹了SpringBoot封裝JDBC使用教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Spring @ComponentScan注解掃描組件原理

    Spring @ComponentScan注解掃描組件原理

    這篇文章主要介紹了Spring @ComponentScan自動(dòng)掃描組件使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • Ribbon單獨(dú)使用,配置自動(dòng)重試,實(shí)現(xiàn)負(fù)載均衡和高可用方式

    Ribbon單獨(dú)使用,配置自動(dòng)重試,實(shí)現(xiàn)負(fù)載均衡和高可用方式

    這篇文章主要介紹了Ribbon單獨(dú)使用,配置自動(dòng)重試,實(shí)現(xiàn)負(fù)載均衡和高可用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 微信公眾平臺(tái)(測(cè)試接口)準(zhǔn)備工作

    微信公眾平臺(tái)(測(cè)試接口)準(zhǔn)備工作

    想要微信開發(fā),首先要有個(gè)服務(wù)器,但是自己沒有。這時(shí)候可以用花生殼,將內(nèi)網(wǎng)映射到公網(wǎng)上,這樣就可以在公網(wǎng)訪問自己的網(wǎng)站了。
    2016-05-05
  • Spring Boot中使用Redis和Lua腳本實(shí)現(xiàn)延時(shí)隊(duì)列的方案

    Spring Boot中使用Redis和Lua腳本實(shí)現(xiàn)延時(shí)隊(duì)列的方案

    通過使用Redis和Lua腳本,可以在Spring Boot環(huán)境中實(shí)現(xiàn)一個(gè)高效且可靠的延時(shí)隊(duì)列系統(tǒng),這種方法利用了Redis的有序集合數(shù)據(jù)結(jié)構(gòu)和Lua腳本的原子性操作來確保任務(wù)的正確性和一致性,這篇文章主要介紹了Spring Boot中使用Redis和Lua腳本實(shí)現(xiàn)延時(shí)隊(duì)列,需要的朋友可以參考下
    2024-05-05
  • Java ApiPost請(qǐng)求返回406狀態(tài)碼問題的解決方案

    Java ApiPost請(qǐng)求返回406狀態(tài)碼問題的解決方案

    APIPost是一款專為開發(fā)者和測(cè)試人員設(shè)計(jì)的API測(cè)試工具,類似于Postman,但提供了更多的團(tuán)隊(duì)協(xié)作和文檔管理功能,它可以幫助你更好地進(jìn)行接口調(diào)試和集成測(cè)試,但遇到了請(qǐng)求后返回的是406狀態(tài),所以本文給大家介紹了Java ApiPost請(qǐng)求返回406狀態(tài)碼問題的解決方案
    2025-04-04
  • Java鎖競(jìng)爭(zhēng)導(dǎo)致sql慢日志原因分析

    Java鎖競(jìng)爭(zhēng)導(dǎo)致sql慢日志原因分析

    這篇文章主要介紹了Java鎖競(jìng)爭(zhēng)導(dǎo)致sql慢的日志原因分析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • springboot如何獲取request請(qǐng)求的原始url與post參數(shù)

    springboot如何獲取request請(qǐng)求的原始url與post參數(shù)

    這篇文章主要介紹了springboot如何獲取request請(qǐng)求的原始url與post參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java實(shí)現(xiàn)搶紅包功能

    Java實(shí)現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)搶紅包功能,采用多線程模擬多人同時(shí)搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • SpringBoot bean查詢加載順序流程詳解

    SpringBoot bean查詢加載順序流程詳解

    當(dāng)你在項(xiàng)目啟動(dòng)時(shí)需要提前做一個(gè)業(yè)務(wù)的初始化工作時(shí),或者你正在開發(fā)某個(gè)中間件需要完成自動(dòng)裝配時(shí)。你會(huì)聲明自己的Configuration類,但是可能你面對(duì)的是好幾個(gè)有互相依賴的Bean
    2023-03-03

最新評(píng)論

无极县| 克东县| 东乡| 景谷| 大方县| 青龙| 双柏县| 车险| 石城县| 蒙山县| 石台县| 突泉县| 云龙县| 呼玛县| 醴陵市| 六盘水市| 兰州市| 库车县| 肇庆市| 龙陵县| 金川县| 商城县| 饶河县| 上杭县| 四会市| 潮州市| 镇坪县| 灵寿县| 隆林| 淄博市| 枣阳市| 邹平县| 缙云县| 西华县| 常州市| 柯坪县| 榕江县| 万山特区| 涪陵区| 宜阳县| 井陉县|