亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 1746 0

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

總結(jié)如下:

Spring配置文件中關(guān)于Spring事務(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. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context = "http://www.springframework.org/schema/context"
  5. xmlns:aop = "http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
  11. < bean id = "sessionFactory"
  12. class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  13. < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
  14. < property name = "configurationClass" value = "org.hibernate.cfg.AnnotationConfiguration" />
  15. </ bean >
  16. <!--定義事務(wù)管理器(聲明式的事務(wù))-->
  17. < bean id = "transactionManager"
  18. class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
  19. < property name = "sessionFactory" ref = "sessionFactory" />
  20. </ bean >
  21. <!--配置DAO-->
  22. < bean id = "userDaoTarget" class = "com.bluesky.spring.dao.UserDaoImpl" >
  23. < property name = "sessionFactory" ref = "sessionFactory" />
  24. </ bean >
  25. < bean id = "userDao"
  26. class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
  27. <!--配置事務(wù)管理器-->
  28. < property name = "transactionManager" ref = "transactionManager" />
  29. < property name = "target" ref = "userDaoTarget" />
  30. < property name = "proxyInterfaces" value = "com.bluesky.spring.dao.GeneratorDao" />
  31. <!--配置事務(wù)屬性-->
  32. < property name = "transactionAttributes" >
  33. < props >
  34. < prop key = "*" > PROPAGATION_REQUIRED </ prop >
  35. </ props >
  36. </ property >
  37. </ bean >
  38. </ beans >

第二種方式:所有Bean共享一個(gè)父類(lèi)bean

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context = "http://www.springframework.org/schema/context"
  5. xmlns:aop = "http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
  11. < bean id = "sessionFactory"
  12. class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  13. < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
  14. < property name = "configurationClass" value = "org.hibernate.cfg.AnnotationConfiguration" />
  15. </ bean >
  16. <!--定義事務(wù)管理器(聲明式的事務(wù))-->
  17. < bean id = "transactionManager"
  18. class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
  19. < property name = "sessionFactory" ref = "sessionFactory" />
  20. </ bean >
  21. < bean id = "transactionBase"
  22. class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  23. lazy-init = "true" abstract = "true" >
  24. <!--配置事務(wù)管理器-->
  25. < property name = "transactionManager" ref = "transactionManager" />
  26. <!--配置事務(wù)屬性-->
  27. < property name = "transactionAttributes" >
  28. < props >
  29. < prop key = "*" > PROPAGATION_REQUIRED </ prop >
  30. </ props >
  31. </ property >
  32. </ bean >
  33. <!--配置DAO-->
  34. < bean id = "userDaoTarget" class = "com.bluesky.spring.dao.UserDaoImpl" >
  35. < property name = "sessionFactory" ref = "sessionFactory" />
  36. </ bean >
  37. < bean id = "userDao" parent = "transactionBase" >
  38. < property name = "target" ref = "userDaoTarget" />
  39. </ bean >
  40. </ beans >

第三種方式:使用攔截器

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context = "http://www.springframework.org/schema/context"
  5. xmlns:aop = "http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
  11. < bean id = "sessionFactory"
  12. class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  13. < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
  14. < property name = "configurationClass" value = "org.hibernate.cfg.AnnotationConfiguration" />
  15. </ bean >
  16. <!--定義事務(wù)管理器(聲明式的事務(wù))-->
  17. < bean id = "transactionManager"
  18. class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
  19. < property name = "sessionFactory" ref = "sessionFactory" />
  20. </ bean >
  21. < bean id = "transactionInterceptor"
  22. class = "org.springframework.transaction.interceptor.TransactionInterceptor" >
  23. < property name = "transactionManager" ref = "transactionManager" />
  24. <!--配置事務(wù)屬性-->
  25. < property name = "transactionAttributes" >
  26. < props >
  27. < prop key = "*" > PROPAGATION_REQUIRED </ prop >
  28. </ props >
  29. </ property >
  30. </ bean >
  31. < bean class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
  32. < property name = "beanNames" >
  33. < list >
  34. < value > *Dao </ value >
  35. </ list >
  36. </ property >
  37. < property name = "interceptorNames" >
  38. < list >
  39. < value > transactionInterceptor </ value >
  40. </ list >
  41. </ property >
  42. </ bean >
  43. <!--配置DAO-->
  44. < bean id = "userDao" class = "com.bluesky.spring.dao.UserDaoImpl" >
  45. < property name = "sessionFactory" ref = "sessionFactory" />
  46. </ bean >
  47. </ beans >

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

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context = "http://www.springframework.org/schema/context"
  5. xmlns:aop = "http://www.springframework.org/schema/aop"
  6. xmlns:tx = "http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd " >
  13. ?
  14. < context:annotation-config />
  15. < context:component-scan base-package = "com.bluesky" />
  16. < bean id = "sessionFactory"
  17. class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  18. < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
  19. < property name = "configurationClass" value = "org.hibernate.cfg.AnnotationConfiguration" />
  20. </ bean >
  21. <!--定義事務(wù)管理器(聲明式的事務(wù))-->
  22. < bean id = "transactionManager"
  23. class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
  24. < property name = "sessionFactory" ref = "sessionFactory" />
  25. </ bean >
  26. < tx:advice id = "txAdvice" transaction-manager = "transactionManager" >
  27. < tx:attributes >
  28. < tx:method name = "*" propagation = "REQUIRED" />
  29. </ tx:attributes >
  30. </ tx:advice >
  31. < aop:config >
  32. < aop:pointcut id = "interceptorPointCuts"
  33. expression = "execution(*com.bluesky.spring.dao.*.*(..))" />
  34. < aop:advisor advice-ref = "txAdvice"
  35. pointcut-ref = "interceptorPointCuts" />
  36. </ aop:config >
  37. </ beans >

第五種方式:全注解

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context = "http://www.springframework.org/schema/context"
  5. xmlns:aop = "http://www.springframework.org/schema/aop"
  6. xmlns:tx = "http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation ="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd " >
  13. ?
  14. < context:annotation-config />
  15. < context:component-scan base-package = "com.bluesky" />
  16. < tx:annotation-driven transaction-manager = "transactionManager" />
  17. < bean id = "sessionFactory"
  18. class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  19. < property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
  20. < property name = "configurationClass" value = "org.hibernate.cfg.AnnotationConfiguration" />
  21. </ bean >
  22. <!--定義事務(wù)管理器(聲明式的事務(wù))-->
  23. < bean id = "transactionManager"
  24. class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
  25. < property name = "sessionFactory" ref = "sessionFactory" />
  26. </ bean >
  27. </ beans >

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

  1. packagecom.bluesky.spring.dao;
  2. importjava.util.List;
  3. importorg.hibernate.SessionFactory;
  4. importorg.springframework.beans.factory.annotation.Autowired;
  5. importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
  6. importorg.springframework.stereotype.Component;
  7. importcom.bluesky.spring.domain.User;
  8. @Transactional
  9. @Component("userDao")
  10. publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao{
  11. publicList < User > listUsers(){
  12. returnthis.getSession().createQuery("fromUser").list();
  13. }
  14. }

?

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 四虎永久免费地址 | 蜜桃精品免费久久久久影院 | 一区二区三区 日韩 | 欧美激情xxxx性bbbb | 毛片网站在线观看 | 99欧美在线 | 国产高清国内精品福利 | 久草精品视频 | 久久久久久久99精品免费观看 | 男人的天堂在线免费视频 | 日本护士一级毛片在线播放 | 国产精品香蕉在线观看首页 | 欧美一级毛片在线观看 | 欧美香蕉视频在线观看 | 欧美亚洲在线 | 一级女性全黄生活片免费看 | 日韩一区二区视频 | 国产一区二区三区不卡在线观看 | 久久久视频6r | 国产午夜精品一区二区三区 | 四虎永久在线视频 | 99精品国产在这里白浆 | 精品无人区乱码一区二区 | 性欧美视频| 91亚洲精品一区二区自 | 免费看美女吃男生私人部位 | 国产成人久久精品激情 | 天天爱天天色天天干 | 欧美在线一级片 | 福利在线网 | 国产在线成人一区二区 | 免费爱爱片 | 亚洲精品ccc | 久久一区不卡中文字幕 | 日韩中文字幕在线不卡 | jizz18性欧美大全 | 亚洲精品美女久久777777 | 国产成人乱码一区二区三区在线 | 亚洲欧美一区二区三区国产精品 | 九九九精品午夜在线观看 | 26uuu色噜噜欧美在线播放 |