Spring与第三方MVC框架集成(Struts)详解

本文介绍了Spring与第三方MVC框架集成时需要考虑的问题,详细阐述了自动加载Spring IOC容器的两种方式,并给出了Spring与Struts集成的具体步骤及两种集成方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一、集成时需要考虑的问题:

        Spring与第三方MVC框架集成时,需要考虑的问题是如何在第三方MVC框架中访问Spring IOC容器管理的Bean,或者说,如何获取Spring IOC容器的实例。我们还要保证在第三方MVC框架开始处理用户请求之前,SpringIOC容器必须初始化完毕。

二、自动加载SpringIOC容器的两种方式

1、利用第三方框架的扩展点,实现加载SpringIOC容器,例如,Struts就提供了Plugin扩展。

2、第二种方式是在web.xml中定义LisenterServlet,让Web应用程序一启动就自动加载SpringIOC容器。这种方式较为通用,对于一些不太常见的第三方MVC框架也可以用这种方式来尝试与Spring集成。

    这里需要注意,如果正在使用基于Servlet 2.3或更高规范的Web服务器,则应当使用Spring提供的ContextLoaderListener来加载IOC容器,因为根据Servlet2.3规范,所有的Listener都会在Servlet被初始化之前完成初始化。由于我们希望尽可能早地完成Spring IOC容器的初始化,因此采用ContextLoaderListener加载SpringIoc容器是最适合的,这时在web.xml中定义如下

 

...

 

<listener>

    <listener-class>

       org.springframework.web.context.ContextLoaderListener

    </listener-class>

</listener>

 

...

 

    默认的,ContextLoaderListener会在/WEB-INF/目录下查找名为applicationContext.xml文件,作为SpringXML配置文件加载。如果使用其他文件名,或者有多个XML配置文件,就需要预先在web.xml文件中的<context-param>标签中指定。

<context-param>

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

       <param-value>

           /WEB-INF/config/spring/applicationContext.xml

           /WEB-INF/config/spring/datasource.xml

           /WEB-INF/config/spring/serviceBean.xml

           /WEB-INF/config/spring/actionBean.xml

       </param-value>

</context-param>

    如果正在使用的Web服务器还不支持Servlet 2.3规范,则无法使用Listener,也就无法通过ContextLoaderListener来加载SpringIOC容器。为些Spring提供了另一个ContextLoaderServlet来加载SpringIOC容器。

<servlet>

       <servlet-name>SpringContextServlet</servlet-name>

       <servlet-class>

           org.springframework.web.context.ContextLoaderServlet

       </servlet-class>

       <load-on-startup>0</load-on-startup>

    </servlet>

    由于必须加载ContextLoaderServlet,然后再加载其他的Servlet,才能保证SpringIoC容器在其他Servlet处理用户请求之前初始化完毕,因此,需设置<load-on-startup>0,表示Web服务器一启动就加载ContextLoaderServlet,其他的Servlet<load-on-startup>值应设得大一些,保证ContextLoaderServlet有足够的时间初始化SpringIOC容器。

一旦完成了Spring IOC容器的加载,就可以通过Spring提供的一个辅助类WebApplicationContextUtils来获得Spring IOC容器的实例了。

ApplicationContext ctx=

WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());

 

三、SpringStruts的集成

方式一:

    第一种方案是通过Spring提供的一个Struts插件来初始化IOC容器,然后从Spring提供的ActionSupport派生所有的Aciton,以便能通过 getWebApplicationContext获得Spring IOC容器中所有Bean的实例。

1、  Struts配置文件的最后添加Spring插件声明:

<struts-config>

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

</struts-config >

    然后设计一个BaseAction,其中定义了获得业务逻辑接口的方法,其他所有的Action都从BaseAction派生即可非常方便地调用业务逻辑接口,比如。

    public class BaseAction extends ActionSupport{

       public Object getBean(String beanName){

    return (Object) getWebApplicationContext().getBean(beanName);

}

}

     这样,BaseAction就可以随时通过SpringApplicationContext获得逻辑组件的引用,所有的Action子类都可以直接通过getBean(String beanName)获得指定组件。

最后一步是编写SpringXML配置文件,在此配置文件中定义所有的Bean组件,但不包括StrutsAction实例。

<beans>

   <bean name="userService" class="com.springTest.UserServiceImpl"

</bean>

...

</beans>

如果SpringXML配置文件没有使用默认的文件名,就必须在Struts配置文件中声明Plugin时指定文件位置。

<struts-config>

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

<set-property property="contextConfigLocation" value="/WEB-INF/config/spring/mySpringConfig.xml "/>

</plug-in>

</struts-config >

 

    这种方式集成SpringStruts时,如果合理地抽象出一个类似BaseAction的类作为所有Action类的超类,在集成到Spring时,只需将BaseAction的超类从StrutsAction类改为Spring提供的ActionSupport,然后在Struts中声明SpringPlugin,就可以在不修改任何Action的情况下实现和Spring的集成。

 

方式二:

    第二种集成Struts的方案是将Struts的所有Action都作为Spring IOC容器中的Bean来管理,然后通过Spring提供的DelegatingRequestProcessor来替代Struts的默认派发器,以便让Spring能截获派发给Action的请求。在这种方式下,业务逻辑组件通过依赖注入的方式在SpringIOC容器中就配置完毕了。

 

    设计一个BaseAction,其中定义了获得业务逻辑接口的方法,其他所有的Action都从BaseAction派生即可非常方便地调用业务逻辑接口,比如。

    public class BaseAction extends Action{

private UserService userService;

public void setUserService(UserService userService){

    this.userService=userService;

}

 

public UserService getUserService(){

    return userService;

}

 

}

 

Struts配置文件的最后添加Spring提供的DelegatingRequestProcessor插件声明:

<struts-config>

...

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

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

</struts-config >

同理,如果SpringXML配置文件没有使用默认的文件名,就必须在Struts配置文件中声明Plugin时指定文件位置。
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property="contextConfigLocation" value="/WEB-INF/config/spring/mySpringConfig.xml "/>
</plug-in>

    Spring提供的Struts插件仍负责启动Spring容器,而DelegatingRequestProcessor可以让Spring接管请求,从而将请求派发到IOC容器管理的Action实例。为此,在SpringXML配置文件中,除了定义业务逻辑和其他组件外,还必须声明每一个Action类,其name属性和Struts配置文件中的path要完全一致。假设在Struts配置文件中配置了两个Action


<action-mappings>

        <action path="/login" type="com.struts.LoginAction"

                name="loginForm" scope="request" validate="true" input="/login.jsp">

            <forward name="success" path="/hello.jsp" />

            <forward name="failure" path="/login.jsp" />

        </action>

        <action path="/logout" type="com.struts.LogoutAction">

            <forward name="success" path="/login.jsp" />

        </action>

    </action-mappings>

    则在Spring的配置文件中,除了定义UserService组件外,还必须定义两个Action,并且要和Struts配置文件中的Action一一对应。

<bean id="userService" class="com.testSpring.UserServiceImpl" />

 

    <bean name="/login" class="com.struts.LoginAction">

        <property name="userService" ref="userService" />

    </bean>

 

    <bean name="/logout" class="com.struts.LogoutAction">

        <property name="userService" ref="userService" />

</bean>

    实际上,使用这种方式实现集成, Struts 配置文件中 Action type 属性将被忽略,因为 Spring 会根据 path 属性查找对应 name Action Bean 。但是,仍然建议将 type 属性标识出来,便于查看 URL Action 的关联。使用这种方式时,由于 Action 被作为 Bean 纳入 Spring IOC 容器管理,因此,可以获得完全的依赖注入能力。不过,最大的不便是所有的 Action 必须同时在 Spring 配置文件和 Struts 配置文件中各配置一次。
    具体选择哪种方式集成要看具体应用,个人认为第二种比较常用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值