You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

Spring is an, among other things, an Inversion of Control framework. As of WebWork 2.2, it is the only supported IoC container. You can find out more about Spring at http://www.springframework.org.

This section covers the only supported Spring integration technique. However, there are many other ways to tie in to Spring with WebWork. Please see Other Spring Integration for more info. Note that none of these other methods are currently supported and could change at any time!

Enabling Spring Integration

Turning on Spring support in WebWork is simply a matter of installing the latest Spring jars in to your classpath and then adding the following entry to webwork.properties:

webwork.objectFactory = spring

At this point, all objects will at least try to get created by Spring. If they cannot be created by Spring, then WebWork will create the object itself. Next, you'll need to turn on the Spring listener in web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Sample Spring Configuration

At this point, you can add the standard Spring configuration at WEB-INF/applicationContext.xml. An example of this configuration is:

<?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="personManager" class="com.acme.PersonManager"/>
    ...
</beans>

Initializing Actions from Spring

Normally, in xwork.xml you specify the class for each action. This means that WebWork will create the action and wire up dependencies as specified as the default auto-wire behavior. However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is remove the class attribute from your WebWork action in xwork.xml and then add a bean in applicationContext.xml.

The only thing to remember when doing this is that there is a special naming convention when looking up actions in Spring. The convention is action:(optional-namespace/)actionName. For example, if you have an action in no namespace ("") called foo and another action in the /secure namespace called bar, your applicationContext.xml would look like so:

<?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="action:foo" class="com.acme.Foo"/>
    <bean id="action:/secure/bar" class="com.acme.Foo"/>
</beans>

Remember: this is not required. This is only needed if you wish to override the default behavior when the action is created in WebWork by decorating it with Spring-enabled interceptors and IoC. Keep in mind that WebWork's Spring integration will do standard IoC, using whatever auto-wiring you specify, even if you don't explicitely map each action in Spring. So typically you don't need to do this, but it is good to know the naming convention if you need to.

  • No labels