2007-08-02
Spring的OpenSessionInViewFilter和OpenSessionInViewInterceptor
关键字: hibernate懒加载 openSessionInViewxml 代码
对Hibernate的懒加载问题,Spring提供了OpenSessionInViewFilter和OpenSessionInViewInterceptor两种东西。
对OpenSessionInViewFilter,需要在web.xml文件中配置。
xml 代码
- <filter>
- <description>处理Hibernate的懒加载问题description>
- <filter-name>OpenSessionInViewFilterfilter-name>
- <filter-class>
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- filter-class>
- <init-param>
- <description>
- 默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里
- 指定这个名字。
- description>
- <param-name>sessionFactoryBeanNameparam-name>
- <param-value>localSessionFactoryparam-value>
- init-param>
- <init-param>
- <description>description>
- <param-name>singleSessionparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>OpenSessionInViewFilterfilter-name>
- <url-pattern>*.dourl-pattern>
- filter-mapping>
默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里指定这个名字。
xml 代码
- <init-param>
- <description>
- 默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里
- 指定这个名字。
- </description>
- <param-name>sessionFactoryBeanName</param-name>
- <param-value>localSessionFactory</param-value>
- </init-param>
其中的sessionFactoryBeanName是你的SessionFactory的实例的名字。这个名字是你在Spring的Bean定义文件中声明的SessionFactory的实例的bean名字。默认这个Filter会使用“sessionFactory”这个值,如果你的SessionFactory确实是这个名字的话,可以不用指明它的名字。但是如果你向我一样,给SessionFactory使用了其他名字,就得在这里声明了。
对OpenSessionInViewInterceptor,就纯粹是Spring IOC容器管理的东西了。
首先,你需要在Spring中声明一个OpenSessionInViewInterceptor的实例
xml 代码
- <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
- <property name="sessionFactory" ref="localSessionFactory" />
- </bean>
然后,你需要在bean urlMapping 中使用这个interceptor:
java 代码
- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="interceptors">
- <list>
- <ref bean="openSessionInViewInterceptor" />
- </list>
- </property>
- <property name="mappings">
- <props>
- <prop key="main.do">mainFrameController</prop>
- </props>
- </property>
- </bean>
在实际应用中,发现一个奇怪的现象,就是使用OpenSessionInViewFilter以后,发现竟然有时候懒加载会出错,因为时间紧迫,还没有找到原因是什么。但是在使用了OpenSessionInViewInterceptor以后,这个问题解决了。
有空的时候再来看到底是什么原因。
- 10:20
- 浏览 (1154)
- 评论 (0)
- 分类: Spring和持久层
- 相关推荐
发表评论
- 浏览: 18854 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
链接
最新评论
-
apachet转发请求到tomcat ...
其实不用改tomcat或apache的文件,程序改为: String par ...
-- by xieboxin -
apachet转发请求到tomcat ...
谢谢了我今天才刚解决了, 以前就是奇怪用tomcat好的,用了apache后还 ...
-- by xuxiangpan888 -
apachet转发请求到tomcat ...
很老的问题了,一般实际开发都是这样做的,至少我们用的tomcat5.0.28是这 ...
-- by jhj823900 -
jBPM的Scheduler模块和Sp ...
提示个方法。。用JobExecutorServlet方法来代替你的Schedul ...
-- by gabriel80 -
spring 学习日记 - 基于 ...
ms可以这样 <bean class="org.springframewo ...
-- by sgfgh






评论排行榜