-----------------------------------------------------------------------
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>