【问题标题】:Spring, Hibernate Lazy Loading, sessionFactory, and OpenSessionInViewFilterSpring、Hibernate 延迟加载、sessionFactory 和 OpenSessionInViewFilter
【发布时间】:2012-01-24 08:43:43
【问题描述】:

我正在使用 Hibernate 的延迟加载,并且得到 sessionFactory 丢失异常,即使我在 web.xml 中定义了一个过滤器以使用 OpenSessionInViewFilter

<?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 属性,但结果没有改变。我做错了什么?

【问题讨论】:

    标签: hibernate spring lazy-loading


    【解决方案1】:

    这是org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 类中查找sessionFactory bean 的代码片段:

    /**
     * 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 的 root WebApplicationContext 的便捷方法。这对于从自定义 Web 视图或 MVC 操作中以编程方式访问 Spring 应用程序上下文很有用。

    请注意,访问根上下文有更方便的方法 对于许多 Web 框架,无论是 Spring 的一部分还是作为 外部图书馆。这个助手类只是最通用的方法 访问根上下文。

    因此,如果您希望使用 Spring 提供的开箱即用的 OpenSessionInViewFilter 功能,则需要在根上下文中声明 sessionFactory

    【讨论】:

      【解决方案2】:

      您必须将 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" />
      

      【讨论】:

      • 我的 session-Factory 已经 in servlet-context.xml。正如@axtavt 建议的那样,我可以通过在 root-context.xml 中定义 sessionFactory 来修复它。如果您能够将 sessionFactory 保留在 servlet-context.xml 中,您会分享您的过滤器定义以及定义它们的文件吗?
      【解决方案3】:

      您需要在根 Web 应用程序上下文(即 /WEB-INF/spring/root-context.xml)中声明 sessionFactory 以使其对 OpenSessionInViewFilter 可用。

      【讨论】:

      • 这行得通。有没有办法将所有过滤器设置也带到 servlet-context.xml,从而不必在 root-context.xml 中定义 sessionfactory?谢谢
      • @Loke: 否。OpenSessionInViewFilter 在请求到达 servlet 之前被调用,因此过滤器无法访问特定于 servlet 的内容。
      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      相关资源
      最近更新 更多