`
cherryQQ
  • 浏览: 1133616 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

牛人论Hibernate-+spring+struts ,Hibernate.initialize(

阅读更多
--------牛人论Hibernate-spring-struts-----------http://www.iteye.com/topic/15057------------------

今天在了解Hibernate.initialize(object)时无意中在网上一个论坛里看到这样的帖子,
这是2005年的旧帖(已经被评为精华贴)
看到这样的帖子真是有点震撼,在2005年就有人对ssh分析的这么透彻,这是令人佩服

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

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

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

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

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

如果使用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配置。

-----------downpour -------发表的评论----发表时间:2005-08-04 ------------------------------------------------------
我原来用struts/spring/hibernate的时候同样使用OpenSessionInView,但是似乎没有robbin所说的问题啊。而且我在使用的时候,是ContextLoaderListener和ContextLoaderPlugIn一起用的。整个配置如下:
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>  
 
..... 

<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> 

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


其余部分省略。

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

不知道robbin所说的ContextLoaderListener和ContextLoaderPlugIn不应该同时使用是不是做得是如下的配置:(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> 

<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中的定义当作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. 
     * <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);;  
    } 

/**
* 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);;
}


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

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

----benber --------------发表时间:2005-12-16 -------------------------------------------------
我也遇到了上面说的openSessionInView不起作用的问题(web.xml既定义了listener,也定义了struts plugin),我想问一下,上面提到的action-servlet.xml到底是什么内容?
在我的应用里spring的配置文件是application-context.xml,它本身是空的,引用spring-data.xml,sping-security.xml等等和存放对应struts action的spring 配置文件spring-struts-action.xml。
struts的配置文件是struts-config.xml,里面定义了所有的action,它们的class都是org.springframework.web.struts.DelegatingActionProxy。最后的plug-in是
Java代码
<plug-in   
className="org.springframework.web.struts.ContextLoaderPlugIn">   
<set-property property="contextConfigLocation"   
value="/WEB-INF/applicationContext.xml"/>   
</plug-in> 

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


结果也遇到了openSessionInView不起作用的问题
在我的应用里都没有出现过action-servlet.xml,我想问下它到底是什么?是对应于我的spring-struts-action.xml还是struts-config.xml引用的一部分?

通俗的说,这个action-servlet.xml到底是spring配置文件还是struts的配置文件?
分享到:
评论

相关推荐

    S2S3H3整合以及泛型Dao与Service封装

    本程序整合了struts-2.2.3.1+spring-framework-3.1.0+hibernate-distribution-3.6.8+JSON+MySQL+Annotation,并且对Dao和Service进行了封装,内含.jar包,并且解决了一对多双向关联的could not initialize proxy - ...

    Spring入门十大问题

    - **AppFuse**:一个基于Spring、Hibernate、Struts等技术的开源项目模板,提供了一种快速搭建Web应用的方法。 - **Spring官方文档**:《Spring Developer Guide》是一本详尽的Spring开发指南,覆盖了Spring的核心...

    jasperreports所有的jar包

    在描述中提到的"能够解决在搭配所有的SSH框架的所有jar包中",暗示了JasperReports可以与Spring、Struts和Hibernate(SSH)这些常见的Java企业级框架无缝集成,提供报表服务。 以下是每个压缩包子文件及其对应的...

    SSH2项目(WeExam)源码

    WeExam=UML(Rose)+Struts2.1.8+Spring2.5+Hibernate3.5+Junit+Mysql 大量使用了Hibernate和Spring的注解 配置步骤: 安装Tomcat及Mysql后: 1、新建WeExam的数据库 2、修改src/jdbc.properties内的连接参数,及...

    ssh框架搭建

    SSH框架搭建是指整合Struts2、Spring和Hibernate这三个开源框架,用于构建高效、灵活的JavaEE应用程序。SSH框架是企业级应用开发中的常见选择,它提供了模型-视图-控制器(MVC)的设计模式,方便数据持久化,以及...

    Prototype-3.pdf

    - **课程内容**:包括Servlets、JSP、Struts、JSF/MyFaces/Facelets、Ajax、GWT等技术,未来还会涵盖Spring和Hibernate等内容。 - **授课地点**:可以在公共场地授课,也可以根据客户需求提供上门服务。 - **授课...

    JAVA错误文档[归纳].pdf

    这是Spring尝试加载Hibernate配置文件失败。确保`hibernate.cfg.xml`存在于类路径中,并且其URL是正确的。 5. **IllegalArgumentException:企图制造saveOrUpdate事件与空的实体** 这个错误通常发生在Hibernate...

    tomcat排错经典案例之404.docx

    ### Tomcat 排错经典案例之 404 错误...通过以上步骤,可以有效地排查和解决在搭建 `Struts2 + Spring + Hibernate` 框架过程中遇到的 404 错误。希望这些信息能帮助开发者们快速定位问题所在,并顺利完成项目开发。

    ssh_inte2模板常用方法&延迟加载问题

    SSH(Spring、Struts2、Hibernate)是一个经典的Java EE开发框架,其中SSH inte2模板是Struts2与Hibernate集成的一种方式。在这个主题中,我们主要关注的是如何在使用SSH inte2模板时处理“延迟加载”问题,以及相关...

    整理后java开发全套达内学习笔记(含练习)

    initialize 预置 初始化 [i'niʃәlaiz] instanceof (关键字) 运算符,用于引用变量,以检查这个对象是否是某种类型。返回 boolean 值。 interface (关键字) 接口 ['intәfeis] invoke vt.调用 [in'vәuk]' ( ...

    maven依赖包(xdw)

    SSH(Struts2、Spring、Hibernate)是Java Web开发中的一个经典框架组合,用于创建企业级应用。 ** Maven 知识点 ** 1. **Maven 构建流程**:Maven 使用约定优于配置的原则,有一个标准的生命周期,包括清理...

    项目管理软件Maven2.0

    在使用Struts2时,Maven2.0可以帮助开发者有效地管理Struts2的依赖,如Struts2核心库、Struts2的插件、以及其他相关库,如Spring、Hibernate等。 1. **添加依赖**:在POM中声明Struts2的依赖,Maven会自动从中央...

    自定义校验注解框架(validator)

    同时,该框架可以与其他验证库如Hibernate Validator集成,以利用其丰富的校验功能。 综上所述,自定义校验注解框架"monkey-validator"可能包括了上述步骤的实现,提供了方便快捷的方式来创建和管理项目特定的参数...

    jrebel插件

    2. **Web项目支持**:对于Web开发者来说,JRebel特别有用,因为它能支持包括Spring、Struts、JSF在内的多种Web框架。即使是对Web-INF下的类、静态文件或JSP页面的修改,都能实时反映在浏览器中,无需重启Tomcat或...

Global site tag (gtag.js) - Google Analytics