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

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)論
主站蜘蛛池模板: 午夜不卡视频 | 免费亚洲视频 | 日日摸夜夜爽久久综合 | 欧美综合国产 | 国产精品久久久久久久久久久威 | 日韩在线a视频免费播放 | 日本爽 | 中国男女全黄大片一级 | 久久精品国产亚洲麻豆小说 | 国产成人久久精品推最新 | 亚洲欧美日韩中文字幕在线一 | 日韩专区亚洲国产精品 | 91精品国产免费久久 | 色视屏| 999久久66久6只有精品 | 色在线网| a资源在线 | 精品亚洲成a人在线播放 | 999奇米 | 女人十八毛片一级毛片免费看 | 亚洲免费在线播放 | 久揄揄鲁一二三四区高清在线 | 操视频网站 | 69欧美另类xxxxx高清 | 日日操夜夜操狠狠操 | 日本免费一级视频 | 欧美性一区二区三区 | 国产香蕉免费精品视频 | 神马我我不卡伦影视 | 精品国产一区二区在线观看 | 99热这里都是精品 | 久久精品99精品免费观看 | 成人精品视频一区二区在线 | 91麻豆精品一二三区在线 | 久久久不卡国产精品一区二区 | 狠狠色噜噜狠狠狠狠888奇米 | 性一级录像 | 九九九精品视频免费 | 久久99精品久久只有精品 | 亚洲人和日本人hd | 国产成人做受免费视频 |