Spring2.5事务管理的三种简单方法

----------------------------------------------------------------------- 
annotation学习继 
----------------------------------------------------------------------- 
Spring2.5事务管理的三种简单方法 

为了在业务类中使用事务管理功能,有如下几个方法: 
1、用原始的TransactionFactoryBean的applicationContext.xml基本配置: 
2、用tx/aop命名空间配置: 
3、使用@Transactional注解配置声明事务(最简单实用的方法): 

在需要事务管理增强的业务类加入@Transactional注解标记 

----------------------------------------------------------------------- 
UserServiceImpl.java 


Java代码 
  1. package service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5. import org.springframework.transaction.annotation.Transactional;  
  6.   
  7. import po.User;  
  8. import dao.UserDao;  
  9.   
  10. @Transactional(readOnly = false)  
  11. //对业务类进行事务增强的标注  
  12. @Service("userService")  
  13. // 声明此类为业务逻辑层的类  
  14. public class UserServiceImpl implements UserService {  
  15.   
  16. @Autowired  
  17. private UserDao userDao;  
  18.   
  19. public void save(User user) {  
  20.    userDao.save(user);  
  21. }  
  22.   
  23. }  

----------------------------------------------------------------------- 

applicationContext.xml 


Xml代码 
  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="  
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  12.   
  13. <bean id="sessionFactory"  
  14.    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  15.    <property name="configLocation">  
  16.     <value>classpath:hibernate.cfg.xml</value>  
  17.    </property>  
  18. </bean>  
  19.   
  20. <!--注解式事务配置驱动-->  
  21. <tx:annotation-driven transaction-manager="transactionManager"  
  22.    proxy-target-class="true" />  
  23.   
  24. <!--业务类bean的实现类标注了@Transactional注解,所以会被  
  25.    tx:annotation-driven注解驱动自动织入事务增强-->  
  26.   
  27. <!-- 配置事务管理器 -->  
  28. <bean id="transactionManager"  
  29.    class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  30.    <property name="sessionFactory">  
  31.     <ref bean="sessionFactory" />  
  32.    </property>  
  33. </bean>  
  34.   
  35.   
  36. <!-- 使Spring关注Annotation -->  
  37. <context:annotation-config />  
  38.   
  39. <!-- 让Spring通过自动扫描来查询和管理Bean -->  
  40. <context:component-scan base-package="dao" />  
  41. <context:component-scan base-package="service" />  
  42.   
  43.   
  44. </beans>  

----------------------------------------------------------------------- 

改写 

----------------------------------------------------------------------- 
UserDaoImpl.java 

Java代码 
  1. package service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5. import org.springframework.transaction.annotation.Propagation;  
  6. import org.springframework.transaction.annotation.Transactional;  
  7.   
  8. import po.User;  
  9. import dao.UserDao;  
  10.   
  11. @Transactional(readOnly = true)  
  12. //对业务类进行事务增强的标注  
  13. @Service("userService")  
  14. // 声明此类为业务逻辑层的类  
  15. public class UserServiceImpl implements UserService {  
  16.   
  17. @Autowired  
  18. private UserDao userDao;  
  19.   
  20. @Transactional(readOnly = false, propagation = Propagation.REQUIRED)  
  21. public void save(User user) {  
  22.    userDao.save(user);  
  23. }  
  24.   
  25. }  

----------------------------------------------------------------------- 
applicationContext.xml 

Xml代码 
  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="  
  8.             http://www.springframework.org/schema/beans  
  9.   
  10. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  11.             http://www.springframework.org/schema/aop  
  12.   
  13. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  14.             http://www.springframework.org/schema/tx  
  15.   
  16. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  17.             http://www.springframework.org/schema/context  
  18.   
  19. http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  20.   
  21. <bean id="sessionFactory"  
  22.    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  23.    <property name="configLocation">  
  24.     <value>classpath:hibernate.cfg.xml</value>  
  25.    </property>  
  26. </bean>  
  27.   
  28. <!--注解式事务配置驱动-->  
  29. <tx:annotation-driven transaction-manager="transactionManager"  
  30.    proxy-target-class="true" />  
  31.   
  32. <!--业务类bean的实现类标注了@Transactional注解,所以会被  
  33.    tx:annotation-driven注解驱动自动织入事务增强-->  
  34.   
  35.   
  36. <!-- 配置事务管理器 -->  
  37. <bean id="transactionManager"  
  38.    class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  39.    <property name="sessionFactory">  
  40.     <ref bean="sessionFactory" />  
  41.    </property>  
  42. </bean>  
  43.   
  44.   
  45. <!-- 使Spring关注Annotation -->  
  46. <context:annotation-config />  
  47.   
  48. <!-- 让Spring通过自动扫描来查询和管理Bean -->  
  49. <context:component-scan base-package="dao" />  
  50. <context:component-scan base-package="service" />  
  51.   
  52.   
  53. </beans>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值