Hibernate/Spring/Struts架构使用OpenSessionInView的...
来源:百度文库 编辑:神马文学网 时间:2024/06/03 06:17:19
检查了一遍Spring的相关源代码,发现了问题的根源:
通常在Web应用中初始化Spring的配置,我们会在web.xml里面配置一个Listener,即:
Xml代码
-
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderListener
如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一个Spring的plugin:ContextLoaderPlugIn。
实际上ContextLoaderListener和ContextLoaderPlugIn的功能是重叠的,他们都是进行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener。
好了,但是你现在既需要Struts集成Spring,又需要OpenSessionInView模式,问题就来了!
由于ContextLoaderListener和ContextLoaderPlugIn功能重叠,都是初始化Spring,你不应该进行两次初始化,所以你不应该同时使用这两者,只能选择一个,因为你现在需要集成Struts,所以你只能使用ContextLoaderPlugIn。
但是令人困惑的是,ContextLoaderListener和ContextLoaderPlugIn有一个非常矛盾的地方!
ContextLoaderListener初始化spring配置,然后把它放在ServletContext对象里面保存:
Java代码
- servletContext.setAttribute(
- WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);;
servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);;
请注意,保存的对象的key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!
但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext对象里面保存:
Java代码
- String attrName = getServletContextAttributeName();;
- getServletContext();.setAttribute(attrName, wac);;
String attrName = getServletContextAttributeName();; getServletContext();.setAttribute(attrName, wac);;
这个attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一样的!
如果仅仅是名字不一样,问题还不大,你仍然可以放心使用ContextLoaderPlugIn,但是当你使用OpenSessionInView的时候,OpenSessionInViewFilter是使用哪个key取得spring配置的呢?
Java代码
- WebApplicationContext wac =
- WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext(););;
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配置。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。 推荐链接
- 限时报名参加Oracle技术大会
- downpour
- 等级:
- 性别:
- 文章: 1301
- 积分: 2048
- 来自: 上海
1.首先是web.xml
Java代码
-
OpenSessionInViewFilter -
class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter class> -
-
OpenSessionInViewFilter -
/* -
-
class>org.springframework.web.context.ContextLoaderListener class> - .....
OpenSessionInViewFilter org.springframework.orm.hibernate.support.OpenSessionInViewFilter OpenSessionInViewFilter /* ...... org.springframework.web.context.ContextLoaderListener
2. 然后是struts-config.xml:
Java代码
"org.springframework.web.struts.ContextLoaderPlugIn"> -
"contextConfigLocation" - value="/WEB-INF/action-servlet.xml"
- />
其余部分省略。
在上述配置下,使用OpenSessionInView似乎没有问题。
不知道robbin所说的ContextLoaderListener和ContextLoaderPlugIn不应该同时使用是不是做得是如下的配置:(struts-config.xml)
Java代码
- className="org.springframework.web.struts.ContextLoaderPlugIn">
"contextConfigLocation" - value="/WEB-INF/applicationContext.xml,
- /WEB-INF/action-servlet.xml"/>
我尝试了一下,用这种配置时,OpenSessionInView的确失效了。
我猜想,原因大概是这样:struts的这个plugIn,可能只是为了整合一个action-servlet.xml,将action-servlet.xml中的定义当作Spring的bean来使用,因此,在保存时,只要有action-servlet.xml的配置,就被保存到robbin所提到的那个attrName中,而不是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE中,所以,OpenSessionInView是取不到这个配置的。
那么这个配置什么时候被取到呢?直觉告诉我,可能是和Action的Proxy有关。于是,查看了org.springframework.web.struts.DelegatingActionProxy的源码,果然:
Java代码
- /**
- * Return the delegate Action for the given mapping.
- *
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);;
- }
/** * Return the delegate Action for the given mapping. *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);; }
仔细看其中的取wac的代码,它并不是从WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE取的wac。
由此,我相信,除了robbin讲的修改源码以外,同时使用ContextLoaderListener和ContextLoaderPlugIn,但是不要在ContextLoaderPlugIn里面加入applicationContext.xml,只要加入你的action-servlet.xml,我相信,同样也可以非常流畅的使用OpenSessionInView