spring 事务管理器的配置
简单阐述:
例如 我的 S2SH_DXL 项目 在该项目中的 dhsfactory.xml 文件中添加事务管理的配置
代码如下:
需要引入
dhsfactory.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: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 "> <!-- 第一种:基于XML的事务管理器 1、 Hibernate事务管理器 2、JDBC事务管理器 --> 1、Hibernate事务管理器 <!-- Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" scope="singleton"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 第一步:定义事务通知(主要是针对指定的事务管理器对应的事务实现的配置事务参数) --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 各种属性,因情况而定使用 <tx:method name="" rollback-for="" no-rollback-for="" timeout="-1" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /> --> <tx:method name="delete" /> <tx:method name="*" /> //*代表所有的方法 </tx:attributes> </tx:advice> <!-- 第二步:AOP配置 --> <aop:config> <!-- 声明事务切入点(配置哪些类的哪些方法参与事务) --> <aop:pointcut id="AllServiceMethod" expression="execution(* cn.dxl.service.*ServiceImpl.*(..))" /> <!-- 通知器(把事务通知绑定到切入点) --> <aop:advisor pointcut-ref="AllServiceMethod" advice-ref="txAdvice" /> </aop:config> 2、JDBC事务管理器 <!-- JDBC事务管理器 (加载驱动时采用JDBC管理器)--> <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- DataSource事务管理器需要数据源实例 --> <property name="sessionFactory" ref="sessionfactory"/> </bean> <!-- 第二种:基于注解的管理器--> <!-- 启用对事务注解的支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
基于注解的事务管理器需要在服务层的实现类中加@Transactional注解 例如: // Account业务逻辑类————基于注解方式的声明式事务管理配置 @Transactional //指定需要声明式事务,事务属性使用默认值 public class UserServiceImpl implements UserService{ private UserServiceDaoImpl userServiceDaoImpl; public void setUserServiceDaoImpl(UserServiceDaoImpl userServiceDaoImpl){ this. userServiceDaoImpl = userServiceDaoImpl; } }