XWork2 allows its "components" like XWork2's action, interceptor, results etc. to be configured using Spring.
Configuration
To configured XWork2 to integrate with Spring, one need to use SpringObjectFactory. XWork2 "components" are created through ObjectFactory. SpringObjectFactory and its subclass are extension of ObjectFactory coded for Spring integration.
static {
ApplicationContext appContext = ....
SpringObjectFactory springObjectFactory = new SpringObjectFactory();
springObjectFactory.setApplicationContext(xmlConfigurationApplicationContext);
springObjectFactory.setAuowiringStrategy(AutowireCapableBeanFactory.AUTOWIRE_BYNAME);
springObjectFactory.setUseClassCache(true);
ObjectFactory.setObjectFactory(springObjectFactory);
}
Hereafter, whenever XWork2 needs an object it will go through ObjectFactory which will allows Spring beans to be returned.
Integration
XWork's Action, Interceptor, Result class name are actually intepreted as Spring beans name. For example,
<xwork ...>
<package ...>
<result-types>
...
<result name="myResult" class="myResultBean" />
...
</result-types>
...
<interceptors>
...
<interceptor name="myInterceptor" class="myInterceptorBean" />
...
</interceptors>
...
<action name="myAction" class="myActionBean">
...
<interceptor-ref name="myInterceptor" />
...
<result name="success" type="myResult" />
...
</action>
</package>
</xwork>
<beans>
<!-- XWork2 Result instantiated, wired using Spring -->
<bean name="myResultBean" class="...." singleton="false">
....
</bean>
<!-- XWork2 Interceptor instantiated, wired using Spring -->
<bean name="myInterceptorBean" class="...." singleton="false">
.....
</bean>
<!-- XWork2 Action instantiated, wired using Spring -->
<bean name="myActionBean" class="...." singleton="false">
....
</bean>
</beans>
|
XWork2 action needs to be configured with singleton="false", cause XWork2 expect Action a new instance for each request / invocation |
|
XWork2 result is preferably configured with singleton="false", unless some information needs to be kept between invocation. By default, without using SpringObjectFactory, a new instance of Result would be created per invocation. |
|
XWork2 interceptors are created once and being repeatably used unless XWork2 is configured to be reloadable. It is preferable that XWork interceptors being configured with singleton="false" as well. |
With this configuration, XWork2's "components" could be instantiated and possibly have its dependencies wired-up through Spring.
|