Spring3 + Hibernate4 配置 No Session found for current thread问题的解决
2013-01-22 13:21
627 查看
在使用Spring mvc + Spring + Hibernate4配置的时候,总是出现 No Session found for current thread,仔细检查applicationContext.xml和dispacter-servlet.xml文件,注解、事务配置都没有问题,纠结好久。
网上搜了很多方法,都不能解决。
有说加上<prop
key="hibernate.current_session_context_class">thread</prop>的配置。有说hibernate4要加上的是<prop
key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>,经过测试,都不能解决。
看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,觉得说的有道理,还是事务没有装配。
试着把dispacter-servlet.xml中的内容合并到[b]applicationContext.xml中,采用一个配置文件(当然web.xml也要做相应修改),发现并没有报错,问题解决。[/b]
[b]
但是自己还是喜欢使用两个配置文件,这样结构更清晰。[/b]
[b]
分析为什么合并到一起就没问题呢,原来[b]spring的context是父子容器,所以会产生冲突,Controller会首先被扫描装配,而此时的Service还没有进行事务的配置,获得的将是原样的Service(没有经过事务装配,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描设备进行事务处理。[/b][/b]
[b]这样就好办了,让两个配置文件各干各的事就可以了。[/b]
1 、在Spring主容器中(applicationContext.xml),用<context:exclude-filter/>将Controller的注解过滤掉,不扫描装配它。
[html] view
plaincopy
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2 而在springMVC配置文件中(dispatcher-servlet.xml)将Service和Dao的注解给[b]过滤掉
,只扫描装配Controller。[/b]
[html] view
plaincopy
<context:component-scan base-package="com">
<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>
网上搜了很多方法,都不能解决。
有说加上<prop
key="hibernate.current_session_context_class">thread</prop>的配置。有说hibernate4要加上的是<prop
key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>,经过测试,都不能解决。
看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,觉得说的有道理,还是事务没有装配。
试着把dispacter-servlet.xml中的内容合并到[b]applicationContext.xml中,采用一个配置文件(当然web.xml也要做相应修改),发现并没有报错,问题解决。[/b]
[b]
但是自己还是喜欢使用两个配置文件,这样结构更清晰。[/b]
[b]
分析为什么合并到一起就没问题呢,原来[b]spring的context是父子容器,所以会产生冲突,Controller会首先被扫描装配,而此时的Service还没有进行事务的配置,获得的将是原样的Service(没有经过事务装配,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描设备进行事务处理。[/b][/b]
[b]这样就好办了,让两个配置文件各干各的事就可以了。[/b]
1 、在Spring主容器中(applicationContext.xml),用<context:exclude-filter/>将Controller的注解过滤掉,不扫描装配它。
[html] view
plaincopy
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2 而在springMVC配置文件中(dispatcher-servlet.xml)将Service和Dao的注解给[b]过滤掉
,只扫描装配Controller。[/b]
[html] view
plaincopy
<context:component-scan base-package="com">
<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>
相关文章推荐
- 关于在Spring中Hibernate的No Session found for current thread问题
- 关于Spring3.2+Hibernate4.1整合问题:No Session found for current thread
- Spring 配置事务管理无效(No Session found for current thread)原因及解决方案
- SpringMVC3+Hibernate4问题:org.hibernate.HibernateException: No Session found for current thread
- spring3 与 Hibernate4集成报错 No Session found for current thread
- Spring-Hibernate - No Session found for current thread
- Spring-hibernate开发时遇到 Could not obtain transaction-synchronized Session for current thread的问题解决
- org.hibernate.HibernateException: No Session found for current thread的解决办法
- SpringMVC @Transactional的陷井大坑引发No Session found for current thread
- Hibernate4 与 spring3 集成之后 “No Session found for current thread”
- No Sessionfound for current thread问题:hibernate.current_session_context_class
- Hibernate4 与 spring3 集成Hibernate4 No Session found for current thread
- 终于解决了:No Session found for current thread
- Spring整合hibernate No Session found for current thread
- Spring 3 和 Hibernate4 整合时遇到的 No Session found for current thread !
- No plugin found for prefix 'spring-boot' ...的问题解决方法
- getCurrentSession()抛出is not valid without active transaction和no session found for current thread异常分析
- HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread