webwork / docs / wikidocs / IoC Configuration.html

<html>
    <head>
        <title>WebWork 2 : IoC Configuration</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            WebWork 2 : IoC Configuration
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jun 18, 2004 by <font color="#0050B2">plightbo</font>.
				    </div>

				    <p class="paragraph"><h2 style="margin: 4px 0px 4px 0px;" class="heading2"><a name="IoCConfiguration-Configurationweb.xml">Configuration - web.xml</a></h2></p>To configure WebWork&#039;s component management, the following lines must be added in the appropriate places to web.xml:<br/>

<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;filter&gt;</span>&#10;    <span class="xml&#45;tag">&lt;filter&#45;name&gt;</span>container<span class="xml&#45;tag">&lt;/filter&#45;name&gt;</span>&#10;    <span class="xml&#45;tag">&lt;filter&#45;class&gt;</span>com.opensymphony.webwork.lifecycle.RequestLifecycleFilter<span class="xml&#45;tag">&lt;/filter&#45;class&gt;</span>&#10;<span class="xml&#45;tag">&lt;/filter&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;filter&#45;mapping&gt;</span>&#10;   <span class="xml&#45;tag">&lt;filter&#45;name&gt;</span>container<span class="xml&#45;tag">&lt;/filter&#45;name&gt;</span>&#10;   <span class="xml&#45;tag">&lt;url&#45;pattern&gt;</span>&#42;.action<span class="xml&#45;tag">&lt;/url&#45;pattern&gt;</span>&#160;<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; modify appropriately &#45;&#45;&gt;</span></span>&#10;<span class="xml&#45;tag">&lt;/filter&#45;mapping&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;listener&gt;</span>&#10;    <span class="xml&#45;tag">&lt;listener&#45;class&gt;</span>com.opensymphony.webwork.lifecycle.SessionLifecycleListener<span class="xml&#45;tag">&lt;/listener&#45;class&gt;</span>&#10;<span class="xml&#45;tag">&lt;/listener&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;listener&gt;</span>&#10;    <span class="xml&#45;tag">&lt;listener&#45;class&gt;</span>com.opensymphony.webwork.lifecycle.ApplicationLifecycleListener<span class="xml&#45;tag">&lt;/listener&#45;class&gt;</span>&#10;<span class="xml&#45;tag">&lt;/listener&gt;</span></pre>
</div></div><p class="paragraph">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&#039;s component management to function correctly.</p><h2 class="heading2"><a name="IoCConfiguration-Configurationxwork.xml">Configuration - xwork.xml</a></h2><p class="paragraph">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 &lt;interceptors&gt; block of xwork.xml as follows:</p><div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;component&quot;</span>&#10;        class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.component.ComponentInterceptor&quot;</span>/&gt;</span></pre>
</div></div><p class="paragraph">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.)<br/>
If you want to apply IoC to objects other than actions or other components, you will need to use the ComponentManager object directly.</p>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.<p class="paragraph"><h2 class="heading2"><a name="IoCConfiguration-Configurationcomponents.xml">Configuration - components.xml</a></h2></p>The components.xml file is used to specify the components that are to be available. The components specified here are loaded into XWork&#039;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).<br/>
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:<br/>

<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;components&gt;</span>&#10;    <span class="xml&#45;tag">&lt;component&gt;</span>&#10;        <span class="xml&#45;tag">&lt;scope&gt;</span>session<span class="xml&#45;tag">&lt;/scope&gt;</span>&#10;        <span class="xml&#45;tag">&lt;class&gt;</span>com.opensymphony.webwork.example.counter.Counter<span class="xml&#45;tag">&lt;/class&gt;</span>&#10;        <span class="xml&#45;tag">&lt;enabler&gt;</span>com.opensymphony.webwork.example.counter.CounterAware<span class="xml&#45;tag">&lt;/enabler&gt;</span>&#10;    <span class="xml&#45;tag">&lt;/component&gt;</span>&#10;<span class="xml&#45;tag">&lt;/components&gt;</span></pre>
</div></div><p class="paragraph">Each component must have the following three attributes:</p><ul class="star">
<li> <em class="emphasis">scope</em>: Valid values are <em class="emphasis">application</em>, <em class="emphasis">session</em> and <em class="emphasis">request</em>. This determines the component&#039;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.</li>
<li> <em class="emphasis">class</em>: This specifies the component&#039;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.</li>
<li> <em class="emphasis">enabler</em>: Any actions that are an instanceof the enabler class or interface will be passed an instance of the component.</li>
</ul><p class="paragraph">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.</p>

				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Oct 15, 2004 02:03</font></td>
		    </tr>
	    </table>
    </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.