我一直在为此绞尽脑汁。我暂时把它放在一边,但现在是时候重新拿起它了。简短的版本是这样的:虽然我在 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>