项目使用springframework 3.2.3.RELEASE,hibernate 4.2.2.Final,使用spring MVC
项目名:exam3
和数据库交互时报错:
- 严重: Servlet.service() for servlet [spring2] in context with path [/exam3] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause
- org.hibernate.HibernateException: No Session found for current thread
- at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
- at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
- at com.dao.UserDao.getByIdentity(UserDao.java:26)
- at com.dao.UserDao.getByIdentityAndStudentID(UserDao.java:42)
- at com.web.controller.LoginController.login(LoginController.java:56)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
- at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
- at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
- at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
- at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
- at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
- at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:662)
页面报错如下:
终于找到原因了,是两个配置文件(beans.xml,spring2-servlet.xml)中context:component-scan配置得有问题.
项目目录结构如下:
spring2-servlet.xml 中的部分代码如下:
- <context:component-scan base-package="com">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
beans.xml中部分代码如下:
- <context:component-scan base-package="com" >
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
值得注意的是:com目录下既有DAO,又有spring MVC的控制器。
解决方案
按如下方式修改之后就OK了:
spring2-servlet.xml:
- <context:component-scan base-package="com.web">
- <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
- </context:component-scan>
beans.xml:
- <context:component-scan base-package="com.dao" >
- <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
- </context:component-scan>
注意:
(1)spring集成hibernate的配置文件是beans.xml;spring MVC的配置文件是spring2-servlet.xml;
(2)以上两个配置文件中均有context:component-scan 标签