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

Spring事務Transaction配置的五種注入方式詳解

 更新時間:2017年04月20日 14:56:56   作者:禮物  
這篇文章主要介紹了Spring事務Transaction配置的五種注入方式詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

前段時間對spring的事務配置做了比較深入的研究,在此之間對Spring的事務配置雖說也配置過,但是一直沒有一個清楚的認識。通過這次的學習發(fā)覺Spring的事務配置只要把思路理清,還是比較好掌握的。

總結如下:

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

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

具體如下圖:

根據(jù)代理機制的不同,總結了五種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.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> 
 
 <!-- 定義事務管理器(聲明式的事務) --> 
 <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.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> 
 
 <!-- 定義事務管理器(聲明式的事務) --> 
 <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.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> 
 
 <!-- 定義事務管理器(聲明式的事務) --> 
 <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.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> 
 
 <!-- 定義事務管理器(聲明式的事務) --> 
 <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> 

 <!-- 定義事務管理器(聲明式的事務) --> 
 <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();
 }  
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • java安全?ysoserial?CommonsCollections1示例解析

    java安全?ysoserial?CommonsCollections1示例解析

    這篇文章主要介紹了java安全?ysoserial?CommonsCollections1示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • SpringBoot接口請求入?yún)⒑统鰠⒃鰪姷奈宸N方法

    SpringBoot接口請求入?yún)⒑统鰠⒃鰪姷奈宸N方法

    這篇文章主要介紹了SpringBoot接口請求入?yún)⒑统鰠⒃鰪姷奈宸N方法,使用`@JsonSerialize`和`@JsonDeserialize`注解,全局配置Jackson的`ObjectMapper`,使用`@ControllerAdvice`配合`@InitBinder`,自定義HttpMessageConverter和使用AOP進行切面編程,需要的朋友可以參考下
    2024-07-07
  • Spring Boot(五)之跨域、自定義查詢及分頁

    Spring Boot(五)之跨域、自定義查詢及分頁

    這篇文章主要介紹了Spring Boot(五)之跨域、自定義查詢及分頁的的相關資料,需要的朋友可以參考下
    2017-04-04
  • Java基礎之Stream流原理與用法詳解

    Java基礎之Stream流原理與用法詳解

    從Java1.8開始提出了Stream流的概念,側重對于源數(shù)據(jù)計算能力的封裝,并且支持序列與并行兩種操作方式。本文就來為大家詳細講講Stream流原理與用法
    2022-08-08
  • Java中的事件處理機制詳解

    Java中的事件處理機制詳解

    這篇文章主要介紹了Java中的事件處理機制詳解,Java事件處理是采取"委派事件模型",當事件發(fā)生時,產(chǎn)生事件的對象,會把此"信息"傳遞給"事件的監(jiān)聽者"處理,這里所說的"信息"實際上就是java.awt.event事件類庫里某個類創(chuàng)建對象,需要的朋友可以參考下
    2023-09-09
  • java實現(xiàn)貪吃蛇游戲代碼(附完整源碼)

    java實現(xiàn)貪吃蛇游戲代碼(附完整源碼)

    這篇文章主要介紹了java實現(xiàn)貪吃蛇游戲代碼(附完整源碼),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • MyBatis-Plus中提供的各種注解詳細說明

    MyBatis-Plus中提供的各種注解詳細說明

    這篇文章主要介紹了MyBatis-Plus中提供的各種注解詳細說明,下面對MyBatis-Plus提供的注解進行整理,本文提供了部分示例代碼以方便理解,需要的朋友可以參考下
    2023-11-11
  • Java8時間日期庫中的常用使用示例

    Java8時間日期庫中的常用使用示例

    這篇文章主要介紹了Java8時間日期庫中的20個常用使用示例,幫助大家更好學習Java8是如何處理時間及日期的方法,感興趣的朋友可以參考一下
    2016-02-02
  • Java實現(xiàn)把excel xls中數(shù)據(jù)轉為可直接插入數(shù)據(jù)庫的sql文件

    Java實現(xiàn)把excel xls中數(shù)據(jù)轉為可直接插入數(shù)據(jù)庫的sql文件

    這篇文章主要介紹了Java實現(xiàn)把excel xls中數(shù)據(jù)轉為可直接插入數(shù)據(jù)庫的sql文件 的相關資料,需要的朋友可以參考下
    2016-03-03
  • hadoop分布式環(huán)境搭建過程

    hadoop分布式環(huán)境搭建過程

    這篇文章主要介紹了hadoop分布式環(huán)境搭建過程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09

最新評論

南陵县| 肇州县| 瑞昌市| 凤台县| 阿瓦提县| 茶陵县| 界首市| 南皮县| 郑州市| 尤溪县| 万荣县| 瓮安县| 青铜峡市| 大宁县| 五家渠市| 德安县| 桃江县| 都兰县| 平湖市| 广安市| 大足县| 乳山市| 拜城县| 南阳市| 新丰县| 宾川县| 崇礼县| 都昌县| 军事| 永川市| 金平| 伊春市| 阿克苏市| 英山县| 自治县| 滦平县| 张掖市| 丰原市| 台北县| 佛山市| 涿鹿县|