Spring事务管理的两种方式
2016-03-20 17:20
483 查看
参考文档:
http://www.iteye.com/topic/1123347 http://blog.csdn.net/lcj8/article/details/2835432[/code]
PS:好像还是tx标签的方式较为简单
tx标签设置拦截器:<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.springsession.demo" /> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- <property name="mapperLocations" value="classpath*:com/springMyBatis/system.mapper/*.xml" /> --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.springsession.demo.dao.ISpringDao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="serviceProxy" class="com.springsession.demo.service.TestService" autowire="byName" /> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用tx标签配置的拦截器 --> <!-- 定义事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 定义切面 --> <aop:config> <aop:pointcut id="interceptorPointcut" expression="execution(* com.springsession.demo.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointcut" /> </aop:config> </beans>
以代理的方式实现事务控制:<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.springsession.demo" /> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- <property name="mapperLocations" value="classpath*:com/springMyBatis/system.mapper/*.xml" /> --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.springsession.demo.dao.ISpringDao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 定义事务 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <!-- 拦截被代理类所有的方法 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置代理对象,利用AOP进行拦截实现对事务的控制 --> <bean id="serviceProxy" parent="transactionProxy"> <property name="target"> <bean class="com.springsession.demo.service.TestService" autowire="byName" /> </property> </bean> </beans>
相关文章推荐
- Struts2学习笔记——Struts2与Spring整合
- Java多线程系列--“JUC线程池”02之 线程池原理(一)
- Spring内部Bean
- Java中HashMap遍历的四种方式
- 20145313张雪纯 《Java程序设计》第3周学习总结
- 深入理解Java之集合框架
- 深入理解Java之反射
- 《Java程序设计》第3周学习总结
- Java - 封装类
- java - 异常处理
- 安卓adb.exe无法启动
- Java中四种引用类型详解
- Java深度遍历文件夹(递归实现)
- JAVA实现最简单的双向链表、队列与栈(内部使用链表形式存储)
- 编译时异常、运行时异常、自定义异常-Java
- java文件的生成及筛选类型文件
- Spring中如何实现Bean引用
- 20145127《java程序设计》第三周学习总结
- myeclipse10配置maven
- java I/O流 文件的生成及筛选