总结xml配置spring-aop声明式事务配置与hibernate报错:** isno active spring和hibernate整合,原因会话工厂去路(到spring不仅仅是bean)错误
spring事務管理太厲害了!!可以不再自管事務開發了!
spring aop聲明式事務配置 問題:
困擾我近十多天的的spring事務管理終于解決了,
再也不用自己管理事務了
嗯,可以刪該死的hibernate事務管理代碼了
分享一下
配置spring aop聲明式事務配置 訪問action時hibernate報錯
*********is not valid without active transaction
這里星號是某個類
的某有事務體現方法
我曾經嘗試了配置4次spring-aop聲明式事務,每次都是好幾個小時,每次最終我都放棄了,現在問題解決了,
我才發現,其實我離配置成功只差一步:就是
在hibernate.cfg.xml
對hibernate框架的有一句配置:
的會話工廠聲明的配置
當然,這在這里是一個錯誤示范。單獨使用hibernate,是正確的
但如果你想整合spring與hibernate的話,就必須改變他
不然就報以上錯誤。
正確格式(hibernate5):
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</property>原因是:
兩者會話工廠的,一開始沒整合,只有hibernate自然,使用原生配置是沒有問題的,
但是一旦與spring框架整合就會有不少問題,
因為這樣整合,其實實際上仍然是hibernate再管理事務,spring也只是幫助注入了會話工廠的bean罷了。并沒有
發揮出她的事務管理特性,那太可惜了
因為你仍然要自己使用hibernate管理事務。
但改了會話去路.current_session_context_class為 : org.springframework.orm.hibernate5.SpringSessionContext就將會話工廠完完全全交給spring了
千萬記住,這是一個大坑,只在spring里配置的話也要注意這個屬性
這樣就好了,當然,也有可能在你的application.xml有其它基礎配置錯誤,那么我再給出springaop的具體配置hibernate的過程
application.xml
注意導入相應spring包,如tx,aop等
aop包:aspectj系列切面必需包,,aop包依賴aspectj
大局的話采用一個bean一個代理會話的配置
<!-- 頭缺beans且注意導入相應spring包---> <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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.5.xsd"> <!-- 注入會話工廠 我使用的是配置文件導入會話工廠 --><!-- HibernateDaoSupport對dao支持類繼承 --><bean id="SessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml" /></bean> <!-- spring-aop --><!-- 定義事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="SessionFactory" /> </bean> <!-- 定義事務 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 定義切面 --> <aop:config> <aop:pointcut id="PointCuts" expression="execution(* com.j.daoimpl.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="PointCuts" /> </aop:config>配置完畢!!!
再來一波,spring真是好框架!
感謝博主maoyuanming0806,他的博文讓我對spring配置的流程更少的盲點,一語驚醒夢中人!!!
并給出原文鏈接:
https://blog.csdn.net/maoyuanming0806/article/details/61417995
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的总结xml配置spring-aop声明式事务配置与hibernate报错:** isno active spring和hibernate整合,原因会话工厂去路(到spring不仅仅是bean)错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hibernate的映射关系配置及对会话
- 下一篇: 关于使用spring管理hibernat