hibernate添加spring 事务管理注意问题记录
今天弄了一天的hibernate添加事務的問題
首先,建立的是一個java工程,把hibernate添加進工程里,很容易就可以寫一個增刪改查的方法。索性就多加點東西,把接口,抽象類也加到里面,自己看著也舒服點,然后寫的時候了,想把spring的功能加進來,想利用spring的aop配置事務,做到事務層配置可控制到service層,這樣的話,就可以專注業(yè)務實現,一個業(yè)務方法就是一個事務。
首先看配置好的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>
????
??
???? <!-- 事務控制在service層 -->
???? <aop:config>
???????? <aop:pointcut id="interceptorPointCuts" expression="execution(* com.wxw.service.*.*(..))" />
???????? <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />???????
???? </aop:config>?????
???
???
</beans>
注意點:
- 自己定義的bean一定要是接口的形式,為什么了,因為spring的配置針對的接口,如果bean配置的實體類,會出現異常
轉載于:https://www.cnblogs.com/wxwall/p/3176809.html
總結
以上是生活随笔為你收集整理的hibernate添加spring 事务管理注意问题记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不作35岁的程序员?
- 下一篇: BZOJ 3329: Xorequ(数位