---
title: "WebWork 2.1 / 2.2 与 Spring 集成方法总结"
author: "csevan"
date: "2008-12-02T09:41:18Z"
tags: []
slug: "webwork-2-1-2-2-spring"
draft: false
lastmod: "2008-12-02T09:41:18Z"
id: 284633
url: "https://csevan.iteye.com/blog/284633"
---

#### 第一部分：WebWork2.1与Spring集成

两种方法：

##### 法一：先WebWork后Spring（利用webwork2-spring.jar集成）

该方法的执行顺序是先WebWork，后Spring。  
即，首先在xwork.xml中找到待执行的action，如果该action依赖其他bean，再到applicationContext.xml中查找被依赖的bean。

WebWork2的IoC功能实现是在xwork.xml中，指定action类与其他bean的依赖关系。即：  
(1)xwork.xml文件  
&lt;action name="myAction" class="com.ryandaigle.web.actions.MyAction"&gt;  
&lt;external-ref name="DAO"&gt;myDAO&lt;/external-ref&gt;  
&lt;result name="success" type="dispatcher"&gt;  
&lt;param name="location"&gt;/success.jsp&lt;/param&gt;  
&lt;/result&gt;  
&lt;/action&gt;

(2)applicationContext.xml文件  
&lt;bean id="myDAO" class="com.ryandaigle.persistence.MyDAO" singleton="true" /&gt;

具体集成步骤如下：

1\. 下载以下文件：  
[http://www.ryandaigle.com/pebble/images/webwork2-spring.jar![](http://192.168.0.59:8080/wiki/images/icons/linkext7.gif)](http://www.ryandaigle.com/pebble/images/webwork2-spring.jar "Visit page outside Confluence")

2\. 配置xwork.xml

2.1 在xwork.xml中定义action时，采用external-ref来指定依赖的spring bean

&lt;action name="myAction" class="com.foo.Action"&gt;  
&lt;external-ref name="someDao"&gt;someDao&lt;/external-ref&gt;

&lt;result name="success" type="dispatcher"&gt;  
&lt;param name="location"&gt;/success.jsp&lt;/param&gt;  
&lt;/result&gt;  
&lt;/action&gt;

2.2 在&lt;package&gt;中指定外部引用解析器

&lt;package name="default" extends="webwork-default"  
externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver"/&gt;

SpringServletContextReferenceResolver作用是在applicationContext.xml中解析外部引用的bean

2.3 增加一个拦截器，允许引用作为外部资源进行解析  
&lt;interceptors&gt;  
&lt;interceptor name="reference-resolver" class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/&gt;  
&lt;interceptor-stack name="myDefaultWebStack"&gt;  
&lt;interceptor-ref name="defaultStack"/&gt;  
&lt;interceptor-ref name="reference-resolver"/&gt;  
&lt;/interceptor-stack&gt;  
&lt;/interceptors&gt;  
&lt;default-interceptor-ref name="myDefaultWebStack"/&gt;

3\. 在web.xml中配置Spring与XWork的外部解析器在同一个Web Context中工作  
&lt;listener&gt;  
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

&lt;listener&gt;  
&lt;listener-class&gt;com.atlassian.xwork.ext.ResolverSetupServletContextListener&lt;/listener-class&gt;  
&lt;/listener&gt;

第一个侦听器是spring必需的，无论是否与WebWork集成，都要定义该侦听器；第二个侦听器是外部解析器所需要的，作用是通过ServletContext来取出Spring的ApplicationContext，作为WebWork与Spring的“桥梁”。

4.在applicationContext.xml中定义被引用的bean  
&lt;bean id="myDAO" class="com.ryandaigle.persistence.MyDAO" singleton="true" /&gt;

说明：  
如果不使用WebWork2，完全使用Spring，达到上述效果，在applicationContext.xml中采取类似定义：  
&lt;bean id="myAction" class="com.ryandaigle.web.actions.MyAction" singleton="false"&gt;  
&lt;property name="DAO"&gt;  
&lt;ref bean="myDAO"/&gt;  
&lt;/property&gt;  
&lt;bean id="myDAO" class="com.ryandaigle.persistence.MyDAO" singleton="true" /&gt;

 

##### 法二：先Spring后WebWork（利用spring-xwork-integration.jar集成）

该方法的执行顺序是先Spring，后WebWork。  
即，首先由Spring负责wire所有的依赖关系，再由XWork来执行action。

注意：因为XWork为每个action调用生成一个新的类实例，action在Spring的applicationContext.xml中应该配置为prototype。

1\. 配置SpringObjectFactory

(0)下载[https://xwork-optional.dev.java.net/files/documents/1475/11992/spring-xwork-integration.jar](https://xwork-optional.dev.java.net/files/documents/1475/11992/spring-xwork-integration.jar)

(1) 可以在web.xml配置

&lt;listener&gt;  
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

&lt;listener&gt;  
&lt;listener-class&gt;com.opensymphony.xwork.spring.SpringObjectFactoryListener&lt;/listener-class&gt;  
&lt;/listener&gt;

(2) 或者：在applicationContext.xml配置  
&lt;bean id="springObjectFactory"  
class="com.opensymphony.xwork.spring.SpringObjectFactory"  
init-method="initObjectFactory"/&gt;

以上两种方法任选一种即可

2\. 在applicationContext.xml中定义bean  
&lt;bean name="some-action" class="fully.qualified.class.name" singleton="false"&gt;  
&lt;property name="someProperty"&gt;&lt;ref bean="someOtherBean"/&gt;&lt;/property&gt;  
&lt;/bean&gt;

3\. 在xwork.xml中定义action（注意action的class与bean的id相同）

&lt;action name="myAction" class="some-action"&gt;  
&lt;result name="success"&gt;view.jsp&lt;/result&gt;  
&lt;/action&gt;

说明：  
该方法与WebWork 2.2中的方法基本相同。

 

#### 第二部分：WebWork2.2与Spring的集成

Spring是WebWork2.2中唯一支持的IoC容器。

1 配置webwork.properties文件，指定spring作为webwork的IoC容器  
webwork.objectFactory = spring  
(1)默认的autowiring模式是：by name  
即如果applicationContext.xml文件中的bean id与xwork.xml文件中的action name相同，就  
(2)如果要改为其他模式：  
webwork.objectFactory.spring.autoWire = name|type|auto|constructor

2 配置web.xml文件，启动Spring侦听器  
&lt;listener&gt;  
&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

3 在WEB-INF目录下增加applicationContext.xml文件  
例：  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;  
&lt;beans default-autowire="autodetect"&gt;  
&lt;bean id="personManager" class="com.acme.PersonManager"/&gt;  
...  
&lt;/beans&gt;

4 设置由Spring来初始化action  
4.1  在Spring的applicationContext.xml文件中配置bean（即action类）  
4.2  将xwork.xml中的action的class属性，由class名改为Spring中定义的bean名  
例如：  
(1)applicationContext.xml中，定义bean id是bar  
&lt;beans default-autowire="autodetect"&gt;  
&lt;bean id="bar" class="com.my.BarClass" singleton="false"/&gt;  
...  
&lt;/beans&gt;  
(2)xwork.xml中，action的class="bar"，而不是通常的类名  
&lt;package name="secure" namespace="/secure" extends="default"&gt;  
&lt;action name="bar" class="bar"&gt;  
&lt;result&gt;bar.ftl&lt;/result&gt;  
&lt;/action&gt;  
&lt;/package&gt;