hibernate添加spring 事务管理注意问题记录
今天弄了一天的hibernate添加事務(wù)的問(wèn)題
首先,建立的是一個(gè)java工程,把hibernate添加進(jìn)工程里,很容易就可以寫一個(gè)增刪改查的方法。索性就多加點(diǎn)東西,把接口,抽象類也加到里面,自己看著也舒服點(diǎn),然后寫的時(shí)候了,想把spring的功能加進(jìn)來(lái),想利用spring的aop配置事務(wù),做到事務(wù)層配置可控制到service層,這樣的話,就可以專注業(yè)務(wù)實(shí)現(xiàn),一個(gè)業(yè)務(wù)方法就是一個(gè)事務(wù)。
首先看配置好的application.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"
?????? 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.1.xsd
?????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
?????? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
?????? ">
??? <bean id="sessionFactory"
??????? class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
??????? <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
??? </bean>
???
???
??? <bean id="iStudentDAO" class="com.wxw.model.StudentDAO">
??????? <property name="sessionFactory" ref="sessionFactory"></property>
??? </bean>
???
???
??? <bean id="studentServiceImpl" class="com.wxw.service.StudentServiceImpl">
??????? <property name="iStudentDao" ref="iStudentDAO"></property>
??? </bean>
???
???
????
????
??? <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
??????? <property name="sessionFactory" ref="sessionFactory"></property>
??? </bean>
???
??? <tx:advice id="txAdvice" transaction-manager="transactionManager">
??????? <tx:attributes>
??????????? <tx:method name="*" propagation="REQUIRED"/>
??????? </tx:attributes>
??? </tx:advice>
????
??
???? <!-- 事務(wù)控制在service層 -->
???? <aop:config>
???????? <aop:pointcut id="interceptorPointCuts" expression="execution(* com.wxw.service.*.*(..))" />
???????? <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />???????
???? </aop:config>?????
???
???
</beans>
注意點(diǎn):
- 自己定義的bean一定要是接口的形式,為什么了,因?yàn)閟pring的配置針對(duì)的接口,如果bean配置的實(shí)體類,會(huì)出現(xiàn)異常
轉(zhuǎn)載于:https://www.cnblogs.com/wxwall/p/3176809.html
總結(jié)
以上是生活随笔為你收集整理的hibernate添加spring 事务管理注意问题记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 不作35岁的程序员?
- 下一篇: JavaScript可否多线程? 深入理