zoukankan      html  css  js  c++  java
  • Spring学习6-Spring整合Struts2

    一、Spring为什么要整合Struts2
        Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了struts2的spring插件包,不需要进行 任何额外的配置,Struts2的Action实例对象将由struts2的spring插件创建,该插件会按照Action的属性名称自动从 spring容器中查找相同名称的bean对象对Action进行装配.
        显然,Struts2的spring插件需要获得spring容器对象,这个spring容器对象是谁创建出来?Struts2的Spring插件又是如何获得Spring容器的呢?在web开发方面,Spring除了提供自己的spring mvc这个web框架之外,它也为web开发提供了最基本的支持,以便在不使用spring mvc的web环境下使用spring,这就是通过 org.springframework.web.context.ContextLoaderListener创建出 WebApplicationContext容器对象, 并将创建出来的WebApplicationContext对象存储进了Web应用程序的application作用域中,存储时的key为 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个常量表示的字符串,以后在Web应用程序中就可以使用 application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) 语句, 或Spring提供的工具方法 WebApplicationContextUtils.getWebApplicationContext(application) 来获得 spring容器对象。ContextLoaderListener使用contextConfigLocation这个ServletContext初 始化参数来指定WebApplicationContext容器对象的配置文件,如果没有配置contextConfigLocation这个 ServletContext的初始化参数,ContextLoaderListener则默认使用/WEB-INF /applicationContext.xml作为配置文件。 为了简便,我们这里采用默认的配置文件,所以,不需要配置contextConfigLocation这个ServletContext的初始化参数,只 需要保证spring配置文件的路径和名称为/WEB-INF/applicationContext.xml即可。


    二、整合步骤
      1、导入struts2的8大核心jar包
       struts2-core-2.1.6.jar   xwork-2.1.2.jar  ognl-2.6.11.jar    freemarker-2.3.13.jar  commons-logging-1.1.jar  commons-fileupload-1.2.1.jar commons-io-2.0.1.jar  javassist-3.11.0.GA.jar

     说明:strut2的8个核心jar包是使用strut2必须导入的最少jar包
     
    2、将struts.xml文件拷贝到src目录下,并配置web.xml
      3、导入Spring的核心jar包
        
    spring-core-3.2.0.M2.jar    spring的核心jar包

      commons-logging.jar

      4、初始化Spring容器,有如下两种方式:
        ①利用ContextLoaderListener类,在Web.xml文件中添加:

    <!– Spring配置信息加载监听器 Context Listener, 必要-->
             <listener>
                <listener-class>
    org.springframework.web.context.ContextLoaderListener
                </listener-class>
            </listener>
        <!-- Spring 防止内存溢出,原因详见:博文底-->
        <listener>
            <listener-class>
                org.springframework.web.util.IntrospectorCleanupListener
            </listener-class>
        </listener>
            该类会在Web启动时自动查找WEB-INF下的applicationContext.xml配置,并且根据该文件来创建Spring容器。当然我们也可以自定义applicationContext.xml的位置,需在web.xml子节点<web-app >的顶端添加:
        <context-param>
           <param-name>contextConfigLocation</param-name>
        <!-- 此位置相对于:部署后的项目的类路径 。多个文件逗号分隔(因这里用到了Spring-Security)-->
           <param-value>
    classpath:applicationContext.xml,classpath:springapplicationContext-security.xml
           </param-value>
        
    <!-- 此位置相对于:部署后的项目的根路径 。多个文件逗号分隔(因这里用到了quartz定时任务框架)-->
        <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/quartz.xml</param-value>
        </context-param>

           
           
       ②如果Web服务器不支持Servlet2.3以上的规范,即不支持Listener利用load-on-startup Servlet创建ApplicationContext,在Web.xml文件中添加:
            <servlet>
                <servlet-name>context</servlet-name>
                <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
            </servlet>
            设置该Servlet随Web服务器的启动而启动,load-on-startup的值越小越早调用。但不管该值多小,Listener都比该Servlet更早启动。
           
       因为ContextLoaderServlet与COntextLoaderListener底层都依赖于ContextLoader,因此二者的效果几 乎没有区别。Listener总比所有的Servlet更优先加载,因此,采用ContextLoaderListener创建 ApplicationContext的时机更早。
     

      5、添加spring与struts2整合的插件
        struts2-spring-plugin-2.1.6.jar(它在strut2框架的jar包中)
     
    千万注意:而不是导入spring框架中的spring-struts-3.2.0.M2.jar,否则在strut.xml中无法使用Spring中Bean工厂的Bean了!!!
    #####
    在项目中包含并应用struts2-spring-plugin-2.1.6.jar 的Struts插件,该插件会把Struts自带的ObjectFactory替换为com/opensymphony/xwork2/spring /SpringObjectFactory,这样所有Action的实例化工作就交给你spring框架,所有的Action既是struts的 action,同时又是spring的bean。在struts的配置中,action的type就不再设置为类型路径,而是bean名称。

     至此整合步骤已完成!!!现在让我们来比较一下整合前后的区别:

    整合之前的struts:

    <package name="test" extends="strutsTest" namespace="/">
            <action name="ActionTest" class="com.test.action.ActionTestAction">
                <result name="input">/ActionTest.jsp</result>
            <result name="success" type="dispatcher">/ActionTest.jsp</result>
        </action>
    </package>

    整合之后的struts:

    <package name="test" extends="strutsTest" namespace="/">
            <action name="ActionTest" class="actionTestAction">
                <result name="input">/ActionTest.jsp</result>
            <result name="success" type="dispatcher">/ActionTest.jsp</result>
        </action>
    </package>

    这里的class属性actionTestAction就是bean的名称:
    <bean id="actionTestAction" class="com.test.action.ActionTestAction"></bean>



    #########################################################
    为什么要在web.xml添加如下代码来防止内存泄露呢:
        <listener>
            <listener-class>
                org.springframework.web.util.IntrospectorCleanupListener
            </listener-class>
        </listener>
       spring的说明如下:
    Listener that flushes the JDK's JavaBeans Introspector cache on web app shutdown. Register this listener in your web.xml to guarantee proper release of the web application class loader and its loaded classes.

    If the JavaBeans Introspector has been used to analyze application classes, the system-level Introspector cache will hold a hard reference to those classes. Consequently, those classes and the web application class loader will not be garbage-collected on web app shutdown! This listener performs proper cleanup, to allow for garbage collection to take effect.

    Unfortunately, the only way to clean up the Introspector is to flush the entire cache, as there is no way to specifically determine the application's classes referenced there. This will remove cached introspection results for all other applications in the server too.

    Note that this listener is not necessary when using Spring's beans infrastructure within the application, as Spring's own introspection results cache will immediately flush an analyzed class from the JavaBeans Introspector cache and only hold a cache within the application's own ClassLoader. Although Spring itself does not create JDK Introspector leaks, note that this listener should nevertheless be used in scenarios where the Spring framework classes themselves reside in a 'common' ClassLoader (such as the system ClassLoader). In such a scenario, this listener will properly clean up Spring's introspection cache.

    Application classes hardly ever need to use the JavaBeans Introspector directly, so are normally not the cause of Introspector resource leaks. Rather, many libraries and frameworks do not clean up the Introspector: e.g. Struts and Quartz.

    Note that a single such Introspector leak will cause the entire web app class loader to not get garbage collected! This has the consequence that you will see all the application's static class resources (like singletons) around after web app shutdown, which is not the fault of those classes!

    This listener should be registered as the first one in web.xml, before any application listeners such as Spring's ContextLoaderListener. This allows the listener to take full effect at the right time of the lifecycle.


    其中JavaBeans Introspector是一个类,位置在Java.bean.Introspector,这个类的用途是发现java类是否符合javaBean规范,也就是这个类是不是javabean。具体用法可以参照jdk文档;

    上面的意思就是,如果有的框架或者程序用到了JavaBeans Introspector了,那么就启用了一个系统级别的缓存,这个缓存会存放一些曾加载并分析过的javabean的引用,当web服务器关闭的时候, 由于这个缓存中存放着这些javabean的引用,所以垃圾回收器不能对web容器中的javaBean对象进行回收,导致内存越来越大。

    spring提供的org.springframework.web.util.IntrospectorCleanupListener就解决了这个问题,他会在web服务器停止的时候,清理一下这个Introspector缓存。使那些javabean能被垃圾回收器正确回收。

    spring不会出现这种问题,因为spring在加载并分析完一个类之后会马上刷新JavaBeans Introspector缓存,这样就保证了spring不会出现这种内存泄漏的问题。

    但是有很多程序和框架在使用了JavaBeans Introspector之后,都没有进行清理工作,比如quartz、struts;解决办法很简单,就是上面的那个配置。这是 因为:对于Web容器来说,最忌讳应用程序私自启动线程,自行进行线程调度,像Quartz这种在web容器内部默认就自己启动了10线程进行异步job 调度的框架本身就是很危险的事情,很容易造成servlet线程资源回收不掉,所以我一向排斥使用quartz。quartz还有一个问题就是不支持 cluster。导致使用quartz的应用都没有办法做群集。

  • 相关阅读:
    Android-自定义meta-data扩展数据
    (转载)【Android】ViewGroup全面分析
    小问题记录
    Gson使用 .Net中的日期序列化成带字母T的格式,Gson发序列化问题
    Android中自定义textview可以进行自体设置
    .Net版的BeanUtils
    Ksoap2-android 添加网络超时的实现
    Html5 viewport 解释
    计算两个日期之间相差的月数
    Android中的Adapter内,有getView方法
  • 原文地址:https://www.cnblogs.com/kabi/p/5182055.html
Copyright © 2011-2022 走看看