Hibernate/Spring/Struts架构使用OpenSessionInView的问题

今天有一个朋友问了我一个问题,他使用的是Hibernate/Spring/Struts架构,配置使用SpringOpenSessionInView Filter,但是发现不生效,lazy的集合属性在页面访问的时候仍然报session已经关闭的错误。我和他一起检查了所有的配置和相关的代码,但是没有发现任何问题。经过调试发现,应用程序使用的SessionOpenSessionInView Filter打开的Session不是同一个,所以OpenSessionInView模式没有生效,但是为什么他们不使用同一个Session呢?

检查了一遍Spring的相关源代码,发现了问题的根源:

通常在Web应用中初始化Spring的配置,我们会在web.xml里面配置一个Listener,即:

xml代码


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

 



如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一个SpringpluginContextLoaderPlugIn

实际上ContextLoaderListenerContextLoaderPlugIn的功能是重叠的,他们都是进行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener

好了,但是你现在既需要Struts集成Spring,又需要OpenSessionInView模式,问题就来了!

由于ContextLoaderListenerContextLoaderPlugIn功能重叠,都是初始化Spring,你不应该进行两次初始化,所以你不应该同时使用这两者,只能选择一个,因为你现在需要集成Struts,所以你只能使用ContextLoaderPlugIn

但是令人困惑的是,ContextLoaderListenerContextLoaderPlugIn有一个非常矛盾的地方!

ContextLoaderListener
初始化spring配置,然后把它放在ServletContext对象里面保存:

java代码


servletContext.setAttribute(
                                        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

 



请注意,保存的对象的keyWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE

但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext对象里面保存:

java代码



String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);

 



这个attrNameWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一样的!

如果仅仅是名字不一样,问题还不大,你仍然可以放心使用ContextLoaderPlugIn,但是当你使用OpenSessionInView的时候,OpenSessionInViewFilter是使用哪个key取得spring配置的呢?

java代码


WebApplicationContext wac =
                                WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

 

 

显然, OpenSessionInViewFilter 是按照 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 这个 key 去拿 spring 配置的!

我们整理一下思路:

ContextLoaderPlugIn
保存 spring 配置的名字叫做 attrName
ContextLoaderListener 保存 spring 配置的名字叫做 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
OpenSessionInView 是按照 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 这个名字去取得 spring 配置的!
而你的应用程序却是按照 attrName 去取得 spring 的配置的!

所以, OpenSessionInView 模式失效!

解决办法:
修改 ContextLoaderPlugIn 代码,在 getServletContext().setAttribute(attrName, wac); 这个地方加上一行代码:
getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

或者修改 OpenSessionInViewFilter ,让它按照 attrName 去取得 spring 配置。

 

*********************************** 

***********************************

 

我原来用struts/spring/hibernate的时候同样使用OpenSessionInView,但是似乎没有robbin所说的问题啊。而且我在使用的时候,是ContextLoaderListenerContextLoaderPlugIn一起用的。整个配置如下:
1.
首先是web.xml

java代码



        <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
    </filter>
   
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   
    <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

......

 



2.
然后是struts-config.xml

java代码



<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property property="contextConfigLocation"
                                  value="/WEB-INF/action-servlet.xml"
        />
</plug-in>

 



其余部分省略。

在上述配置下,使用OpenSessionInView似乎没有问题。

不知道robbin所说的ContextLoaderListenerContextLoaderPlugIn不应该同时使用是不是做得是如下的配置:(struts-config.xml

java代码



<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>

 



我尝试了一下,用这种配置时,OpenSessionInView的确失效了。

我猜想,原因大概是这样:struts的这个plugIn,可能只是为了整合一个action-servlet.xml,将action-servlet.xml中的定义当作Springbean来使用,因此,在保存时,只要有action-servlet.xml的配置,就被保存到robbin所提到的那个attrName中,而不是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE中,所以,OpenSessionInView是取不到这个配置的。

那么这个配置什么时候被取到呢?直觉告诉我,可能是和ActionProxy有关。于是,查看了org.springframework.web.struts.DelegatingActionProxy的源码,果然:

java代码



/**
        * Return the delegate Action for the given mapping.
        * <p>The default implementation determines a bean name from the
        * given ActionMapping and looks up the corresponding bean in the
        * WebApplicationContext.
        * @param mapping the Struts ActionMapping
        * @return the delegate Action
        * @throws BeansException if thrown by WebApplicationContext methods
        * @see #determineActionBeanName
        */
        protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
                WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
                String beanName = determineActionBeanName(mapping);
                return (Action) wac.getBean(beanName, Action.class);
        }

        /**
        * Fetch ContextLoaderPlugIn's WebApplicationContext from the
        * ServletContext, containing the Struts Action beans to delegate to.
        * @param actionServlet the associated ActionServlet
        * @param moduleConfig the associated ModuleConfig
        * @return the WebApplicationContext
        * @throws IllegalStateException if no WebApplicationContext could be found
        * @see DelegatingActionUtils#getRequiredWebApplicationContext
        * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
        */
        protected WebApplicationContext getWebApplicationContext(
                        ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {
                return DelegatingActionUtils.getRequiredWebApplicationContext(actionServlet, moduleConfig);
        }

 

 

 

来源:http://forum.javaeye.com/viewtopic.php?t=15057

仔细看其中的取 wac 的代码,它并不是从 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 取的 wac

由此,我相信,除了 robbin 讲的修改源码以外,同时使用 ContextLoaderListener ContextLoaderPlugIn ,但是不要在 ContextLoaderPlugIn 里面加入 applicationContext.xml ,只要加入你的 action-servlet.xml ,我相信,同样也可以非常流畅的使用 OpenSessionInView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值