Spring声明式事务配置管理方法
来源:互联网 发布:网络项目管理培训课程 编辑:程序博客网 时间:2024/05/07 22:46
1、首先在/WEB-INF/applicationContext.xml添加以下内容:
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
注:这是作为公共使用的事务管理器Bean。这个会是事先配置好的,不需各个模块各自去配。
2、下面就开始配置各个模块所必须的部分,在各自的applicationContext-XXX-beans.xml配置的对于事务管理的详细信息。
<!-- 配置事务传播特性 -->
<tx:advice id="TestAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="del*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="add*" propagation="REQUIRED"/><tx:method name="find*" propagation="REQUIRED"/><tx:method name="get*" propagation="REQUIRED"/><tx:method name="apply*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置参与事务的类 -->
<aop:config><aop:pointcut id="allTestServiceMethod" expression="execution(* com.test.testAda.test.model.service.*.*(..))"/><aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" /></aop:config>需要注意的地方:
(1) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。
(2) tx:attribute标签所配置的是作为事务的方法的命名类型。
如<tx:method name="save*" propagation="REQUIRED"/>
其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。
propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
(3) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。
首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:
expression="execution(* com.test.testAda.test.model.service.*.*(..))"
其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。
(4) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法(一)
- Spring声明式事务配置管理方法(二)
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- 配置ssh密钥认证自动登录(从物理机ssh到虚拟机)
- 调用系统拍照功能
- LinkedList中定义一个堆栈或者队列
- MYSQL 加密SQL
- 车载系统之 Windows CE 应用软件框架设计
- Spring声明式事务配置管理方法
- HDU 5033 Building
- 机床行业:机床业复苏已现星星之火,三线索布局未来
- 联合主键
- 导入项目出现红色感叹号
- 返回上一级页面并刷新,上级页面显示关系上上级页面参数
- 《李航:统计学习方法》笔记之感知机
- Android自动化测试(UiAutomator)简要介绍
- Java自定义注解和运行时靠反射获取注解