---
title: "Struts2和Spring整合"
author: "mutex_js"
date: "2011-03-05T21:00:58Z"
tags: []
categories: ["企业架构"]
slug: "struts2-spring"
draft: false
lastmod: "2011-03-05T21:00:58Z"
id: 941756
url: "https://mutex-js.iteye.com/blog/941756"
---

Spring整合Struts2的两种方案 收藏
一、需要的JAR文件为：Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件，比如commons-logging.jar等等，另外还需要Struts2发布包中的struts2-spring-plugin-x.xx.jar。

二、在web.xml中增加WebApplicationContext的相应配置，以下两种配置方式本质是一样的。
1.         Servlet 2.3及以上版本可以使用监听器，相应配置如下：
<context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果spring配置文件被命名为applicationContext.xml，并且放在WEB-INF目录下，则不需要配置<context-param>，因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件。若存在多个Spring配置文件，则在<param-value>中依次列出，之间以逗号隔开。
2.         Servlet 2.3以下版本由于不支持<listener>，需要配置<servlet>，格式如下：
<context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
              <servlet-name>contextLoaderServlet</servlet-name>
              <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
</servlet>
如果spring配置文件被命名为applicationContext.xml，并且放在WEB-INF目录下，则不需要配置<context-param>，因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件，或者是名字为contextConfigLocation的ServletContext参数所指定的文件。由于该Servlet配置只是为了在容器启动时能启动ContextLoaderServlet使其工作，而不需要引用该Servlet，所以不需要配置<servlet-mapping>。
三、在web.xml中完成加载WebApplicationContext之后，接下来就可以做到Spring和Struts2的整合了。整合有两种方法，分别叙述如下：
1.         第一种实现方法：
1)        将Struts的业务逻辑控制器类配置在Spring的配置文件中，业务逻辑控制器中引用的业务类一并注入。注意，必须将业务逻辑控制器类配置为scope=”prototype”！
示例如下：
<bean id=”LoginAction” class=”yaso.struts.action.LoginAction”>
                     <property name=”loginDao” ref=”LoginDao”/>
</bean>
2)        在struts.xml或者等效的Struts2配置文件中配置Action时，指定<action>的class属性为Spring配置文件中相应bean的id或者name值。示例如下：
<action name=”LoginAction” class=”LoginAction”>
                     <result name=”success”>/index.jsp</result>
</action>
2.         第二种实现方法：
1)        业务类在Spring配置文件中配置，业务逻辑控制器类不需要配置，Struts2的Action像没有整合Spring之前一样配置，<action>的class属性指定业务逻辑控制器类的全限定名。
2)        业务逻辑控制器类中引用的业务类不需要自己去初始化，Struts2的Spring插件会使用bean的自动装配将业务类注入进来，其实业务逻辑控制器也不是Struts2创建的，而是Struts2的Spring插件创建的。默认情况下，插件使用by name的方式装配，可以通过增加Struts2常量来修改匹配方式：设置方式为：struts.objectFactory.spring.autoWire = typeName，可选的装配参数如下：
a)         name：等价于Spring配置中的autowire=”byName”，这是缺省值。
b)        type：等价于Spring配置中的autowire=”byType”。
c)         auto：等价于Spring配置中的autowire=”autodetect”。
d)        constructor：等价于Spring配置中的autowire=” constructor”。
四、如果原先在Struts2中使用了多个object factory，则需要通过Struts2常量显式指定object factory，方式如下：struts.objectFactory = spring；如果没有使用多个object factory，这一步可以省略。
五、可以通过设增加Struts2常量来指定是否使用Spring自身的类缓存机制。可以设定的值为true或false，默认为true。设置方式为：struts.objectFactory.spring.useClassCache = false。
六、至此，完成了两种方式的整合。比较这两种整合方式，其本质是一样的。不同之处在于，使用第二种自动装配的方式时，由于没有在Spring中配