Apache Struts 2 Documentation > IoC Configuration |

Apache Struts 2 Documentation
IoC Configuration
Edit Page Browse Space Add Page Add News
Added by gjz22, last edited by Ted Husted on Feb 04, 2007 (view change)

| These documents are out of date. As of WebWork 2.2, the WebWork IoC container has been deprecated (though not removed) and the WebWork team recommends you use Spring Plugin for all your IoC needs

## Configuration - web.xml

To configure WebWork's component management, the following lines must be added in the appropriate places to web.xml:

    <filter>
        <filter-name>container</filter-name>
        <filter-class>com.opensymphony.webwork.lifecycle.RequestLifecycleFilter</filter-class>
    </filter>

    <filter-mapping>
       <filter-name>container</filter-name>
       <url-pattern>*.action</url-pattern> <!-- modify appropriately -->
    </filter-mapping>

    <!-- Optionally you may instead apply the filter to EVERY URI instead of just *.action.  -->
    <!-- You might want to do this for example:  -->
    <!-- * your page flow goes to a jsp directly (as opposed to only *.action URIs) -->
    <!-- * the jsp has an action in it to be run with the ww:action tag -->
    <!-- * the action in the jsp implements any enabler interfaces to get components served to it -->
    <!-- The reason: (Per Patrick Lightbody) -->
    <!-- "The components work by looking for a ComponentManager in the request -->
    <!-- attributes. It gets placed there by a filter. If it is on *.action and a -->
    <!-- request comes in through a JSP, the filter won't be applied and it will -->
    <!-- never work." -->
    <!-- The overhead in doing this is small, so don't worry overly much about the  -->
    <!-- performance of this. -->
    <!-- <filter-mapping> -->
    <!--    <filter-name>container</filter-name> -->
    <!--    <url-pattern>*</url-pattern>  -->
    <!-- </filter-mapping> -->

    <listener>
        <listener-class>com.opensymphony.webwork.lifecycle.SessionLifecycleListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.opensymphony.webwork.lifecycle.ApplicationLifecycleListener</listener-class>
    </listener>

These settings allow WebWork to manage components across the application, session and request scopes. Note that even if one or more of the scopes are not required by your application, all three scopes need to be specified in web.xml for WebWork's component management to function correctly.

## Configuration - xwork.xml

The ComponentInterceptor class is used to apply the IoC pattern to XWork actions (ie, to supply components to actions). The ComponentInterceptor should be declared in the <interceptors> block of xwork.xml as follows:

    <interceptor name="component"
            class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>

You should ensure that any actions that are to be supplied with components have this interceptor applied. (See OS:XWork Interceptors for information on how to apply interceptors to actions.)
If you want to apply IoC to objects other than actions or other components, you will need to use the ComponentManager object directly.

Note too, that the ComponentInterceptor is applied as part of the webwork defaultStack. Thus, if you are applying the defaultStack to the action, you would include the ComponentInterceptor.

## Configuration - components.xml

The components.xml file is used to specify the components that are to be available. The components specified here are loaded into XWork's ComponentManager and are then made available to any actions that are an instance of the specified enabler. The components.xml file must be placed in the root of the classpath (ie, in the WEB-INF/classes directory).
Here is an example components.xml file that configures a Counter component. The Counter object will live in session scope, and will be passed to any objects that are enabled due to their implementing the CounterAware interface:

    <components>
        <component>
            <scope>session</scope>
            <class>com.opensymphony.webwork.example.counter.Counter</class>
            <enabler>com.opensymphony.webwork.example.counter.CounterAware</enabler>
        </component>
    </components>

Each component must have the following three attributes:

- scope: Valid values are application, session and request. This determines the component's lifetime. Application scope components will be created when the webapp starts up, and they will survive for the whole lifetime of the webapp. Session scoped components exist for the duration of a user session, while components in request scope only last for the duration of a single client request.
- class: This specifies the component's class. An instance of this object will live for the duration of the specified scope, and will be made available to any actions (or other code) as required. Note that components are lazy-loaded, so if nothing makes use of the component during its lifetime, the component will never actually be instantiated. At the moment components must have a zero argument constructor.
- enabler: Any actions that are an instanceof the enabler class or interface will be passed an instance of the component.

Note that while components are allowed to have dependencies on other components they must not depend on another component that is of a narrower scope. So for example, a session component cannot depend on a component that is only of request scope.