2018-2-5
结合https://www.cnblogs.com/biobio/p/7754080.html
http://blog.csdn.net/frankcheng5143/article/details/51308344
成功解决问题。
前言
在整合springmvc4+spring4+hibernate5时,DAO层在getCurrentSession()时报错:Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
错误跟踪到sessionFactory.getCurrentSession();
protected Session getCurrentSession() {
return this.sessionFactory.getCurrentSession(); }
解决方案
原理都在上面两个链接,我也是看的一脸懵逼,不过我直接把方法给出,只需要修改两个文件,也就是applicationContext.xml和springmvc.xml,下面就是我的成功配置。
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
applicationContext.xml(只扫描dao层和Service层)
<!-- 扫描路径,不扫描Controller --> <context:component-scan base-package="casic.bj(改为你的包名)"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
-------------------------------------------------------------------------------
springmvc.xml中(只扫描@controller)
<context:component-scan base-package="casic.bj(改为你的包名)"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>