我正在使用Hibernate的延迟加载,并且即使在web.xml中定义了一个使用OpenSessionInViewFilter的过滤器后,也正在获取sessionFactory缺少的异常
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
我的servlet-context.xml具有以下会话和事务管理器定义:
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <beans:property name="dataSource" ref="dataSource"/> <beans:property name="hibernateProperties"> <beans:props> <beans:prop key="hibernate.dialect">${connection.dialect}</beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> <beans:property name="annotatedClasses"> <beans:list> <beans:value>example.EntityA</beans:value> <beans:value>example.EntityB</beans:value> </beans:list> </beans:property> </beans:bean> <beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <beans:property name="sessionFactory" ref="sessionFactory"/> </beans:bean> <tx:annotation-driven transaction-manager="transactionManager" />
我仍然收到以下异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
我尝试定义一个sessionFactoryBeanName属性,但结果没有改变。我究竟做错了什么?
您需要sessionFactory在根Web应用程序上下文中进行声明(即/WEB-INF/spring/root- context.xml)以使其可用于OpenSessionInViewFilter。
sessionFactory
/WEB-INF/spring/root- context.xml
OpenSessionInViewFilter