Versions Compared

Key

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

Table of Contents

Description

Info

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

...

Code Block
titlestruts.xml

<struts>
  <constant name="struts.objectFactory" value="spring" />
  ... 
</struts>

...

  • Configure the Spring listener
Code Block
xml
xml
titleweb.xmlxml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  • Register your objects via the Spring configuration
Code Block
xml
xml
titleapplicationContext.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" scope="prototype"/>
    ...
</beans>
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.

...

Your struts.xml file would then have the Action class attributes changed.

Code Block
xml
xml
titlestruts.xmlxml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>

    <package name="default" extends="struts-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>
</struts>

...

A typical spring configuration for bar could look as following.

Code Block
xml
xml
titleapplicationConext.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="bar" class="com.my.BarClass" singleton="false"/>
    ...
</beans>

...

  1. Set "struts.devMode" to "true"
  2. Set "struts.class.reloading.watchList" to a comma separated list of directories, or jar files (absolute or relative paths)
  3. Add this to web.xml:

    Code Block
    typexml
    
       <context-param>
           <param-name>contextClass</param-name>
           <param-value>org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext</param-value>
       </context-param> 
    
  4. Add Apache Commons JCI FAM to the classpath. If you are using maven, add this to pom.xml

    Code Block
    typexml
    
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-jci-fam</artifactId>
           <version>1.0</version>
       </dependency> 
    

Letting the reloading class loader handle all the classes can lead to ClassCastException(s) because instances of the same classes loaded by different class loaders can not be assigned to each other. To prevent this problem we suggest that struts.class.reloading.acceptClasses is used to limit the classes loaded by the reloading class loader, so only actions are handled by it. This constant supports a comma separated list of regular expressions:

Code Block
typexml

<constant name="struts.class.reloading.acceptClasses" value="com\.myproject\.example\.actions\..*" />

...

Setting

Description

Default

Possible Values

struts.objectFactory.spring.autoWire

The autowire strategy

name

name,type,auto, or constructor

struts.objectFactory.spring.autoWire.alwaysRespect

Whether the autowire strategy should always be used, or if the framework should try to guess the best strategy based on the situation

false for backwards-compatibility

true or false

struts.objectFactory.spring.useClassCache

Whether to have Spring use its class cache or not

true

true or false

struts.class.reloading.watchList

List of jar files or directories to watch for changes

null

Comma separated list of absolute or relative paths to jars or directories

struts.class.reloading.acceptClasses

List of regular expressions of accepted class names

null

Comma separated list of regular expressions of classes that will be loaded by the reloading class loader(we suggest to add regular expressions so only action classes are handled by the reloading class loader)

struts.class.reloading.reloadConfig

Reload the runtime configuration (action mappings, results etc) when a change is detected in one of the watched directories

false

true or false

DEPRECATED: struts.objectFactory.spring.enableAopSupport

Uses different logic to construct beans to allow support AOP, it uses an old approach to create a bean, switch this flag if you have problems with Spring beans and AOP

false

true or false

Installation

...