1

我一直在为此绞尽脑汁。我暂时把它放在一边,但现在是时候重新拿起它了。简短的版本是这样的:虽然我在 dispatcher-servlet.xml 文件中配置了 OpenSessionInViewInterceptor,但我仍然得到了可怕的 LazyIntializationException。

现在,关于细节。

我在 dispatcher-servlet.xml 中配置了 OSIV 拦截器,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-lazy-init="true">

<import resource="applicationContext.xml" />

<!-- This MUST be defined here or the DispatcherServlet will act a bit funky -->
<context:component-scan base-package="org.me.app.web.controller" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />


<!-- Convenient way to map URLs to JSPs w/o having a Controller -->
<mvc:view-controller path="/index.html" view-name="index" />
<mvc:view-controller path="/UserAgreement.html"
    view-name="UserAgreement" />

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/*.html" />
        <bean
            class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

Hibernate SessionFactory 和 TransactionManager bean 在常规 applicationContext.xml 文件中定义:

<tx:annotation-driven />
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        <tx:method name="load*" read-only="true" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut expression="execution(* *.manager.*Manager.*(..))" id="serviceOperations"/>
    <aop:advisor advice-ref="transactionAdvice" pointcut-ref="serviceOperations" />
</aop:config>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibername.format_sql">true</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>

        </props>
    </property>
    <property name="packagesToScan" value="org.me.app.model" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

使用 Spring 3.1.1 和 Hibernate 4.1。

任何帮助,将不胜感激。

杰森

编辑:根据建议,我尝试切换到过滤器而不是拦截器。我注释掉了 dispatch-servlet.xml 的整个部分,并将以下内容添加到 web.xml(还显示了其他过滤器和过滤器映射):

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml
            classpath:security.xml
    </param-value>
</context-param>

<filter>
    <filter-name>osiv</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<!-- This filter checks to see if the user has accepted terms -->
<filter>
    <filter-name>acceptTermsFilter</filter-name>
    <filter-class>org.me.web.filter.terms.AcceptTermsFilter</filter-class>
</filter>


<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>osiv</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>

<filter-name>acceptTermsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4

1 回答 1

0

我假设您正在使用 OSIV 来扩展 jsp 视图中的关系 - 如果是这种情况,那么拦截器可能无法工作,因为拦截器在呈现视图之前完成了它们的处理,在这种特定情况下,在 jsp 之前由容器呈现的休眠会话将被关闭。

更好的方法可能是使用 OSIV 过滤器——org.springframework.orm.hibernate4.support.OpenSessionInViewFilter它也将支持 jsp 中的惰性关系。

更新:我注意到的一件事是您在 dispatcher-servlet.xml 文件中导入了 applicationContext.xml,最好不要在 dispatcher-servlet.xml 中导入 applicationContext.xml,而是使用 ContextLoaderListener 加载它:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

然后正常加载 dispatcher-servlet.xml 的其余部分。我之所以这么说是因为只有通过这条路线才能将其root application context注册为单例并且可供sessionFactoryOSIV 过滤器检测到休眠状态。你能也试试这个,看看效果如何

于 2012-06-05T20:54:24.917 回答