我正在使用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属性,但结果不会改变。我做错了什么?
答案 0 :(得分:3)
您需要在根网络应用程序上下文中声明sessionFactory
(即/WEB-INF/spring/root-context.xml
),以便OpenSessionInViewFilter
可以使用它。
答案 1 :(得分:0)
您必须将sesstionFactory Bean放在servlet-context.xml
中<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
.....
</property>
<property name="hibernateProperties">
......
</property>
<property name="schemaUpdate" value="true" />
答案 2 :(得分:0)
这是查找org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
bean的sessionFactory
类中的代码片段:
/**
* Look up the SessionFactory that this filter should use.
* <p>The default implementation looks for a bean with the specified name
* in Spring's root application context.
* @return the SessionFactory to use
* @see #getSessionFactoryBeanName
*/
protected SessionFactory lookupSessionFactory() {
if (logger.isDebugEnabled()) {
logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
}
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
}
您会注意到它使用WebApplicationContextUtils
类来加载会话工厂bean。此类的API(http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/support/WebApplicationContextUtils.html)声明:
检索给定ServletContext的根WebApplicationContext 的便捷方法。这对于从自定义Web视图或MVC操作中以编程方式访问Spring应用程序上下文非常有用。
请注意,有更方便的方法来访问根上下文 对于许多Web框架,可以是Spring的一部分,也可以作为一个 外部图书馆。这个助手类只是最通用的方法 访问根上下文。
因此,如果您希望使用Spring提供的sessionFactory
功能,则需要在根上下文中声明OpenSessionInViewFilter
。