Spring2.5事务管理的三种简单方法
来源:互联网 发布:淘宝店铺图片轮播 编辑:程序博客网 时间:2024/05/21 03:18
-----------------------------------------------------------------------
annotation学习继
-----------------------------------------------------------------------
Spring2.5事务管理的三种简单方法
为了在业务类中使用事务管理功能,有如下几个方法:
1、用原始的TransactionFactoryBean的applicationContext.xml基本配置:
2、用tx/aop命名空间配置:
3、使用@Transactional注解配置声明事务(最简单实用的方法):
在需要事务管理增强的业务类加入@Transactional注解标记
-----------------------------------------------------------------------
UserServiceImpl.java
-----------------------------------------------------------------------
applicationContext.xml
-----------------------------------------------------------------------
改写
-----------------------------------------------------------------------
UserDaoImpl.java
-----------------------------------------------------------------------
applicationContext.xml
annotation学习继
-----------------------------------------------------------------------
Spring2.5事务管理的三种简单方法
为了在业务类中使用事务管理功能,有如下几个方法:
1、用原始的TransactionFactoryBean的applicationContext.xml基本配置:
2、用tx/aop命名空间配置:
3、使用@Transactional注解配置声明事务(最简单实用的方法):
在需要事务管理增强的业务类加入@Transactional注解标记
-----------------------------------------------------------------------
UserServiceImpl.java
- package service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import po.User;
- import dao.UserDao;
- @Transactional(readOnly = false)
- //对业务类进行事务增强的标注
- @Service("userService")
- // 声明此类为业务逻辑层的类
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- public void save(User user) {
- userDao.save(user);
- }
- }
-----------------------------------------------------------------------
applicationContext.xml
- <?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/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
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <!--注解式事务配置驱动-->
- <tx:annotation-driven transaction-manager="transactionManager"
- proxy-target-class="true" />
- <!--业务类bean的实现类标注了@Transactional注解,所以会被
- tx:annotation-driven注解驱动自动织入事务增强-->
- <!-- 配置事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <!-- 使Spring关注Annotation -->
- <context:annotation-config />
- <!-- 让Spring通过自动扫描来查询和管理Bean -->
- <context:component-scan base-package="dao" />
- <context:component-scan base-package="service" />
- </beans>
-----------------------------------------------------------------------
改写
-----------------------------------------------------------------------
UserDaoImpl.java
- package service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import po.User;
- import dao.UserDao;
- @Transactional(readOnly = true)
- //对业务类进行事务增强的标注
- @Service("userService")
- // 声明此类为业务逻辑层的类
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
- public void save(User user) {
- userDao.save(user);
- }
- }
-----------------------------------------------------------------------
applicationContext.xml
- <?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/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
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <!--注解式事务配置驱动-->
- <tx:annotation-driven transaction-manager="transactionManager"
- proxy-target-class="true" />
- <!--业务类bean的实现类标注了@Transactional注解,所以会被
- tx:annotation-driven注解驱动自动织入事务增强-->
- <!-- 配置事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <!-- 使Spring关注Annotation -->
- <context:annotation-config />
- <!-- 让Spring通过自动扫描来查询和管理Bean -->
- <context:component-scan base-package="dao" />
- <context:component-scan base-package="service" />
- </beans>
- Spring2.5事务管理的三种简单方法
- spring2.5 实例化bean的三种方法
- spring2.5系列之---------三种实例化Bean的方法
- Spring2事务管理的两种配置(XML&标注
- Spring2事务管理的两种配置(XML&&标注)
- 事务管理(三)——JDBC事务管理的简单实现
- SPRING JDBC事务管理的三种配置方法
- Spring2.5的注解配置简单介绍
- spring2.5整合ibatis2.3事务管理时遇到的问题以及需要的jar包
- Spring事务管理的三种方式
- Spring事务管理的三种方式
- Spring事务管理的三种方式
- spring1.x与spring2声明式事务管理的配置
- Spring2.0事务管理接入点的写法 execution
- 传智播客Spring2.5视频教程_Spring的三种实例化Bean的方式 1
- 传智播客Spring2.5视频教程_Spring的三种实例化Bean的方式 2
- 学习笔记---------------------spring2.5+jdbc(jdbctemplate)+事务管理
- Spring2.5基于注解和XML配置事务管理
- skinmagic使用方法
- 黯然神伤
- 在CentOS 5 Linux环境下配置TurboGears 2.1
- 女儿与数独(女儿24个月)
- 改变 CListCtrl、CHeaderCtrl 高度、字体、颜色和背景
- Spring2.5事务管理的三种简单方法
- C++ STL 容器技术 之 deque双端队列容器(很全面)
- 栈的链表实现 与 数组实现
- Silverlight 4 初试 - Hello World
- 经典文章收集
- void 指针 void*
- 设计模式之单例模式
- 三种类型的ARP病毒
- vs2008 编译和反编译