日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Spring事務(wù)配置的五種方式

前段時(shí)間對(duì)Spring的事務(wù)配置做了比較深入的研究,在此之間對(duì)Spring的事務(wù)配置雖說(shuō)也配置過(guò),但是一直沒(méi)有一個(gè)清楚的認(rèn)識(shí)。通過(guò)這次的學(xué)習(xí)發(fā)覺(jué)Spring的事務(wù)配置只要把思路理清,還是比較好掌握的。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比施甸網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式施甸網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋施甸地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

總結(jié)如下:

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

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

具體如下圖:

Spring事務(wù)配置

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

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

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.     
  19.    
  20.     
  21.     
  22.         
  23.     
  24.    
  25.     
  26.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
  27.             
  28.                
  29.          
  30.          
  31.          
  32.          
  33.              
  34.                 PROPAGATION_REQUIRED
  35.              
  36.          
  37.      

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

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.     
  19.    
  20.     
  21.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
  22.             lazy-init="true" abstract="true"> 
  23.          
  24.          
  25.          
  26.          
  27.              
  28.                 PROPAGATION_REQUIRED 
  29.              
  30.          
  31.        
  32.   
  33.     
  34.     
  35.         
  36.     
  37.    
  38.      
  39.           
  40.     

第三種方式:使用攔截器

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.      
  19.   
  20.     
  21.         class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
  22.          
  23.          
  24.          
  25.              
  26.                 PROPAGATION_REQUIRED 
  27.              
  28.          
  29.     
  30.      
  31.      
  32.          
  33.              
  34.                 *Dao
  35.              
  36.          
  37.          
  38.              
  39.                 transactionInterceptor 
  40.              
  41.          
  42.      
  43.  
  44.     
  45.     
  46.         
  47.     

第四種方式:使用tx標(biāo)簽配置的攔截器

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:tx="http://www.springframework.org/schema/tx"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7.            http://www.springframework.org/schema/context
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  11.     
  12.     
  13.     
  14.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  15.          
  16.         
  17.      
  18.      
  19.     
  20.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  21.         
  22.     
  23.     
  24.         
  25.             
  26.         
  27.     
  28.    
  29.     
  30.         
  31.             expression="execution(* com.bluesky.spring.dao.*.*(..))" />
  32.         
  33.             pointcut-ref="interceptorPointCuts" />       
  34.          

第五種方式:全注解

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:tx="http://www.springframework.org/schema/tx"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7.            http://www.springframework.org/schema/context
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  11.     
  12.     
  13.     
  14.     
  15.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  16.          
  17.         
  18.      
  19.      
  20.     
  21.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  22.         
  23.     
  24.    

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

 
 
 
 
  1. package com.dawnsky.spring.dao;
  2. import java.util.List;
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  6. import org.springframework.stereotype.Component;
  7. import com.bluesky.spring.domain.User;
  8. @Transactional
  9. @Component("userDao")
  10. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  11.     public List listUsers() {
  12.         return this.getSession().createQuery("from User").list();
  13.     }
  14. }

本文標(biāo)題:Spring事務(wù)配置的五種方式
文章地址:http://www.5511xx.com/article/dphpdhd.html