---
title: "使用spring整合struts总结"
author: "y3320987"
date: "2010-03-09T11:04:43Z"
tags: []
categories: ["企业架构"]
slug: "spring-struts"
draft: false
lastmod: "2010-03-09T13:54:09Z"
id: 611042
url: "https://complex.iteye.com/blog/611042"
---

**1、使用ActionSupport([示例下载](http://j-complex.appspot.com/download.jsp?param=j-sr2-actionsupport) )**

       org.springframework.web.struts.ActionSupport 类提供了一个getWebApplicationContext() 方法，可用于手动创建一个 Spring 环境。

       这种方式很简单并且易于理解。但将 Struts Action与 Spring 框架耦合在一起。如果想替换掉 Spring，那么必须重写代码。 并且，由于 Struts Action不在 Spring 的控制之下，所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时，这种方式可能有用， 但是在大多数情况下，这种方法不如另外两种方法合适。

 

**2、覆盖 RequestProcessor** **([示例下载](http://j-complex.appspot.com/download.jsp?param=j-sr2-requestprocessor) )**

       使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts  
的 RequestProcessor 处理程序，可将 Spring 从 Struts Action 中分离出来。在struts-config中的配置如下：

```
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="csntextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>
```

       优点是由于 Struts Action 注意不到 Spring 的存在，所以不需要重写 Struts 代码就可以使用其他控制反转容器来替换掉 Spring。

 

 

**3、将Action管理委托给Spring** **([示例下载](http://j-complex.appspot.com/download.jsp?param=j-sr2-delegate) )**

       通过在struts-config中注册Action时使用org.springframework.web.struts.DelegatingActionProxy类来实现，该方式的优点如下：  
             a)：没有 Spring 的话，所有的 Struts 动作都必须是线程安全的。如果设置 &lt;bean&gt; 标记的 singleton 属性   为“false”，那么不管用何种方法，应用程序都将在每一个请求上有一个新生成的动作对象。  
             b)：可以利用 Spring 的生命周期方法。例如，当实例化 Struts 动作时，&lt;bean&gt; 标记的 init-method 属性 被用于运行一个方法。类似地，在从容器中删除 bean 之前，destroy-method 属性执行一个方法。这些方法是管理昂贵对象的好办法，它们以一种与 Servlet 生命周期相同的方式进行管理。  
              c)：Struts Action不了解 Spring，不对代码作任何改变就可用于非 Spring 应用程序中。RequestProcessor 的改变不会影响它，并且它可以利用 Spring AOP 特性的优点。