Webwork 與spring集成研究
一、 webwork與spring的集成方式
webwork2.2中引入了新的webwork與spring的集成方式,方法如下:
1) 將spring.jar 添加到classpath中
2) 在webwork.properties中做如下改動(dòng):
webwork.objectFactory = spring,spring默認的對象裝配模式是通過(guò)name,如果想改變,webwork.objectFactory.spring.autoWire = type
3) 在web.xml中添加如下內容
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
4) 創(chuàng )建一個(gè)spring的配置文件ApplicationContext,其位置信息可以添加到web.xml中,如下
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
通常,我們在xwork.xml中未每一個(gè)action指定了類(lèi),當使用了SpringObjectFactory后,webwork會(huì )要求spring來(lái)創(chuàng )建action并根據指定的自動(dòng)裝配方式來(lái)為action裝配屬性。SpringObjectFactory同時(shí)也會(huì )在bean上應用所有的后續處理,比如代理action(為了控制事務(wù)),安全性等等。
參考:We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions. This includes making your beans able to be autowired by either naming your dependent properties on your action the same as the bean defined in Spring which should be provided (to allow for name-based autowiring), or using autowire-by-type and only having one of the required type registered with Spring. It also can include using JDK5 annotations to declare transactional and security requirements rather than having to explicitly set up proxies in your Spring configuration. If you can find ways to let Spring know what it needs to do for your action without needing any explicit configuration in the Spring applicationContext.xml, then you won‘t have to maintain this configuration in both places.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean id="bar" class="com.my.BarClass" singleton="false"/>
...
</beans>
注意: the id attribute in the spring configuration corresponds to the class attribute in the xwork configuration. Also note that in the spring configuration, the singleton attribute is set to false. This would generally be the case that is desired as Webwork creates a new action class upon each request. Hence when Spring integration is used, this would be the desired behaviour. Making Springs singleton attribute false would allow this.
注意,因為webwork的action是線(xiàn)程安全的,對于每一個(gè)request,webwork都會(huì )創(chuàng )建一個(gè)新的action實(shí)例來(lái)處理。但是spring默認的bean創(chuàng )建方式是采用單例模式的,即在默認方式下,spring會(huì )創(chuàng )建一個(gè)bean對象,然后服務(wù)于所有的請求。要在兩者之前達成一致,最好將singleton="false"
二、 webwork與spring的分工
1、 webwork的工作,總得來(lái)說(shuō)是提供一個(gè)流程的控制:
Ø 通過(guò)UI tag接收用戶(hù)請求,將基于http的請求參數和相關(guān)的環(huán)境變量封裝到通用的ActionContext中
Ø Xwork接收到webwork傳遞過(guò)來(lái)的actionContext,放入valueStack
Ø 請求spring將創(chuàng )建action對象,裝配action的屬性(SpringObjectFactory)
Ø 執行攔截器before()方法,執行action的execute()方法,執行攔截器after()方法
Ø 執行result,返回給客戶(hù)端結果
2、 Spring的工作,主要是解決對象之間的依賴(lài)問(wèn)題
Ø 創(chuàng )建action對象,為其裝配相關(guān)的類(lèi)成員變量,比如Dao類(lèi),事務(wù)控制
三、 對于事務(wù)以及其他spring特性的使用
聯(lián)系客服