spring 事物管理配置
来源:互联网 发布:mac 命令行打开文件夹 编辑:程序博客网 时间:2024/05/01 22:39
- 首先在/WEB-INF/applicationContext.xml添加以下内容:
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
注:这是作为公共使用的事务管理器Bean。这个会是事先配置好的,不需各个模块各自去配。
- 下面就开始配置各个模块所必须的部分,在各自的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配置hibernate事物管理
- spring配置里面的事物管理
- spring+springMVC声明事物管理配置
- Spring配置之hibernate事物管理AOP
- spring事物管理
- spring事物管理
- spring 事物管理
- spring事物管理
- Spring + mybatis事物管理
- spring事物管理
- spring事物管理
- Spring的事物管理
- Spring 事物管理 @Transactional
- Spring的事物管理
- 怎么在用asp代码来创建Access数据库
- VTK学习笔记-2-TIFF图像数据的重切片
- ubuntu下离线手动安装软件包 例: libncurses5-dev
- 《c专家编程》笔记--define和typedef的区别
- 还有一个月,我不会放弃
- spring 事物管理配置
- 第六周—1.改错
- JavaCC使用小结
- [正能量系列]失业的程序员(一)
- Unix/Linux下的open函数(O_CREAT和O_EXCL)
- php基础学习
- 黑马程序员-交通灯管理系统
- to_date to_timestamp to_char cast的一些总结
- poj 1745