`
jiagyao
  • 浏览: 95684 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Spring整合Struts (转)

阅读更多
Spring整合Struts
虽然Spring也提供了自己的MVC组件,但一来Spring的MVC组件过于繁琐,二     来Struts的拥护者实在太多。因此,很多项目都会选择使用Spring整合Struts框架。而且Spring确实可以无缝整合Struts框架,二者结合成一个更实际的J2EE开发平台。

利用Struts的PlugIn来启动Spring容器
使用Spring的Web应用时,不用手动创建Spring容器,而是通过配置文件声明式地创建Spring容器。因此,在Web应用中创建Spring容器有如下两个方式:

  ● 直接在web.xml文件中配置创建Spring容器。

  ● 利用第三方MVC框架的扩展点,创建Spring容器。

其实第一种创建Spring容器的方式更加常见。为了让Spring容器随Web应用的启动而自动启动,有如下两个方法:

  ● 利用ServletContextListener实现。

  ● 采用load-on-startup Servlet实现。

Spring 提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为Listener使用,会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此,如果只有一个配置文件,并且文件名为 applicationContext.xml,只需在web.xml文件中增加如下配置片段即可:

<listener>

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

</listener>

如果有多个配置文件需要载入,则考虑使用<context-param>元素来确定配置文件的文件名。 ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context- param时,参数名字应该是contextConfigLocation。

带多个配置文件的web.xml文件如下:

<?xml version="1.0" encoding="GBK"?>

<!-- 指定Web配置文件的根元素,以及相应的Schema信息 -->

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

    version="2.4">

    <!-- 确定多个配置文件 -->

    <context-param>

        <!-- 参数名为contextConfigLocation -->

        <param-name>contextConfigLocation</param-name>

        <!-- 多个配置文件之间以“,”隔开 -->

        <param-value>/WEB-INF/daoContext.xml,/WEB-INF/
        applicationContext.xml</param-value>

    </context-param>

    <!-- 采用listener创建ApplicationContext实例 -->

    <listener>

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

    </listener>

</web-app>

如果没有通过contextConfigLocation指定配置文件,Spring会自动查找application- Context.xml配置文件;如果有contextConfigLocation,则利用该参数确定的配置文件。如果无法找到合适的配置文件, Spring将无法正常初始化。

Spring 根据bean定义创建WebApplicationContext对象,并将其保存在web应用的ServletContext中。大部分情况下,应用中的Bean无须感受到ApplicationContext的存在,只要利用ApplicationContext的IoC即可。

如果需要在应用中获取ApplicationContext实例,可以通过如下代码获取:

//获取当前Web应用的Spring容器

WebApplicationContext ctx =

    WebApplicationContextUtils.getWebApplicationContext(servletContext);

除此之外,Spring提供了一个特殊的Servlet类ContextLoaderServlet。该Servlet在启动时,会自动查找WEB-INF/下的applicationContext.xml文件。

当然,为了让ContextLoaderServlet随应用的启动而启动,应将此Servlet配置成load-on-startup的Servlet,load-on-startup的值小一点比较合适,这样可以保证Application- Context更快的初始化。

如果只有一个配置文件,并且文件名为applicationContext.xml,在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用于提供“后台”服务,主要用于创建Spring容器,无须响应客户请求,因此无须配置servlet-mapping。

如果有多个配置文件,一样使用<context-param>元素来确定多个配置文件。

事实上,不管是ContextLoaderServlet,还是ContextLoaderListener,都依赖于ContextLoader创建ApplicationContext实例。

在ContextLoader代码的第240行,有如下代码:

String configLocation = servletContext.getInitParameter
(CONFIG_LOCATION_PARAM);

if (configLocation != null) {

    wac.setConfigLocations(StringUtils.tokenizeToStringArray
    (configLocation,

    ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));

}

其中,CONFIG_LOCATION_PARAM是该类的常量,其值为contextConfigLocation。可以看出, ContextLoader首先检查servletContext中是否有contextConfigLocation的参数,如果有该参数,则加载该参数指定的配置文件。

ContextLoaderServlet 与ContextLoaderListener底层都依赖于ContextLoader。因此,二者的效果几乎没有区别。之间的区别不是它们本身引起的,而是由于Servlet规范,Listener比Servlet优先加载。因此,采用ContextLoaderListener创建 ApplicationContext的时机更早。

当然,也可以通过ServletContext的getAttribute方法获取ApplicationContext。但使用 WebApplicationContextUtils类更便捷,因为无须记住ApplicationContext的属性名。即使 ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRI- BUTE属性没有对应对象,WebApplicationContextUtils的getWebApplicationContext()方法将会返回空,而不会引起异常。

到底需要使用Listener,还是使用load-on-startup Servlet来创建Spring容器呢?通常推荐使用Listener来创建Spring容器。但Listerner是Servlet 2.3以上才支持的标准,因此,必须Web容器支持Listener才可使用Listerner。

注意:使用Listener创建Spring容器之前,应先评估Web容器是否支持Listener标准。

还有一种情况,利用第三方MVC框架的扩展点来创建Spring容器,比如Struts。在第2章介绍Strust框架时,知道Struts有一个扩展点PlugIn。

实际上,Spring正是利用了PlugIn这个扩展点,从而提供与Struts的整合。Spring提供了PlugIn接口的实现类 org.springframework.web.struts.ContextLoaderPlugIn。这个实现类可作为Struts的PlugIn 配置,Struts框架启动时,将自动创建Spring容器。

为了利用Struts的PlugIn创建Spring容器,只需在Struts配置文件中增加如下片段   即可:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

  <set-property property="contextConfigLocation"

      value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.
      xml"/>

</plug-in>

其中,指定contextConfigLocation属性值时,即可以指定一个Spring配置文件的位置,可以指定多个Spring配置文件的位置。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics