在这个问题上我一直在绞尽脑汁。我暂时把它放在一边,但是现在是时候再把它拿起来了。简短的版本是这样的:虽然我在我的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>
答案 0 :(得分:0)
我假设您正在使用OSIV在jsp视图中扩展关系 - 如果是这种情况,则拦截器可能无法工作,因为拦截器在呈现视图之前完成其处理,在此特定情况下jsp由容器呈现,hibernate会话将被关闭。
更好的方法可能是使用OSIV过滤器 - org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
,它也将支持jsp中的惰性关系。
更新:我注意到的一件事是你有一个applicationContext.xml,它在你的dispatcher-servlet.xml文件中导入,最好不要在dispatcher-servlet中导入applicationContext.xml .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
被注册为单身,并且可供OSIV过滤器检测到的hibernate sessionFactory
。你能不能试一试,看看它是怎么回事