Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:

Update formatting
Info
titleAbout Spring

Spring is

...

a lightweight container, providing centralized, automated configuration and wiring of your application objects, using a technique called "Dependency Injection"

(tick) Spring integration is built into framework. In fact, Spring is the default object factory for SAF 2.

Spring Integration

Spring integration is enabled by default. The default object factory is set in the action.properties files.

Code Block
titleaction.properties
action
Note

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 the 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 action.properties:

Code Block
webwork.objectFactory = spring

Autowiring

The framework enables "autowiring" by default. (Autowiring means to look for objects If you want to change from the default autowiring mode, which is to auto-wire by name (i.e. to look for beans defined in Spring with the same name as your bean object property), then you'll also need a setting for this in your action.properties:. To change the wiring mode, modify the spring.autowire property.

Code Block
titleWiring Mode
action
Code Block
webwork.objectFactory.spring.autoWire = type

Options for this setting are:The autowire property can be set to several options.

name

Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default

type

Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring

auto

Spring will attempt to auto-detect the best method for auto-wiring your action

constructor

Spring will auto-wire the parameters of the bean's constructor

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

Enabling Spring integration for other application objects is a two-step process.

  • Configure the Spring listener
Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than applicationContext.xml.
Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern:
Code Block
xml
xml
titleweb.xml
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Tip
titleMore ApplicationContext configuration files needed?
  • Register your objects via the Spring configuration

See the Spring documentation for a full description of this parameter.

Code Block
xml
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>

Sample Spring Configuration

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

titleapplicationContext.xml
Code Block
xmlxml
<?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>

Switching from Builtin IoC to Spring

Switching is quite easy. Spring setup is done as described above. To complete migration, you will have to

  1. transfer your configured components from components.xml to applicationContext.xml appropriately. You can safely delete components.xml afterwards.
  2. remove the Component Interceptor from your interceptor stack in action.xml. Although it does not hurt to leave it there, it is simply redundant from now on.
Tip
titleMore applicationContext configuration files needed?

Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than applicationContext.xml. Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern:

Code Block
xml
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>

See the Spring documentation for a full description of this parameter

Note
titleSession Scope & Spring

Spring <= 1.3 does not support session scoped components. Spring 2.0 release will add support for this, and tests with Spring 2.0M3 (beta) are reported to work quite well. Please refer to Spring Session Components Workarounds to read more about this topic.

Initializing Actions from Spring

Normally, in xwork action.xml you specify the class for each actionAction. When using the SpringObjectFactory (configured as shown above) WebWork default SpringObjectFactory, the framework will ask Spring to create the action Action and wire up dependencies as specified by the default auto-wire behavior. The SpringObjectFactory will also apply all bean post processors to do things like proxy your action for transactions, security, etc. which Spring can automatically determine without explicit configuration. For most usages, this should be all you need for configuring your actions to have services and dependencies applied.

Tip

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.

However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply more complex AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your WebWork action Action in the xwork action.xml to use the bean name defined in Spring instead of the class name.

Your xwork action.xml file would then have the action Action class attributes changed, leaving it like this:.

Code Block
xml
xml
titleaction.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.1.dtd">

<xwork>
    <include file="webwork-default.xml"/>

    <package name="default" extends="webwork-default">
        <action name="foo" class="com.acme.Foo">
            <result>foo.ftl</result>
        </action>
    </package>

    <package name="secure" namespace="/secure" extends="default">
        <action name="bar" class="bar">
            <result>bar.ftl</result>
        </action>
    </package>
</xwork>

Where you have a Spring bean defined in your applicationContext.xml named "bar". Note that the com.acme.Foo action Action did not need to be changed, because it can be autowired.

A typical spring configuration for bar could look as following.

Note the
Code Block
xml
xml
langtitleapplicationConext.xml
<?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>
Note
TitleTake Note

How the code works

  • The id attribute in the

...

  • Spring configuration corresponds to the class attribute in the

...

  • action configuration.

...

  • The singleton attribute is set to false

...

  • , meaning that Spring will create a new Action class upon each request

...

  • , as SAF would do.
Warning
titleSpring Actions are Optional!

Remember: registering Actions with Spring

...

is not required.

...

The Spring alternative is there if you need it, but the framework will automatically create Actions objects from the action mappings. But, if you want to use Spring to inject your Actions, the option is there.