<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Web App Solution Blog &#187; spring</title>
	<atom:link href="http://www.webappsolution.com/wordpress/category/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webappsolution.com/wordpress</link>
	<description>When you're in need of an appsolution</description>
	<pubDate>Thu, 05 Apr 2012 19:39:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActionScript Dependency Injection With RemoteClass Metadata Issue With Fix - UPDATED</title>
		<link>http://www.webappsolution.com/wordpress/2009/08/31/actionscript-dependency-injection-with-remoteclass-metadata-issue-with-fix/</link>
		<comments>http://www.webappsolution.com/wordpress/2009/08/31/actionscript-dependency-injection-with-remoteclass-metadata-issue-with-fix/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 03:07:49 +0000</pubDate>
		<dc:creator>brianr</dc:creator>
		
		<category><![CDATA[actionscript]]></category>

		<category><![CDATA[blazeds]]></category>

		<category><![CDATA[cairngorm]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[dependency-injection]]></category>

		<category><![CDATA[java-rpc]]></category>

		<category><![CDATA[register-class-alias]]></category>

		<category><![CDATA[remote-object]]></category>

		<category><![CDATA[remoteclass-metadata]]></category>

		<guid isPermaLink="false">http://www.webappsolution.com/wordpress/?p=507</guid>
		<description><![CDATA[NOTE: My fix might be a little overkill, so I offer a simpler approach at the bottom of this post&#8230;and since this post still contains some good info on dynamically mapping classes at runtime leveraging the registerClassAlias() method, I decided to leave it as is sans this simple note.
Say you&#8217;re using Spring ActionScript (SAS) or [...]]]></description>
			<content:encoded><![CDATA[<p><strong>NOTE</strong>: My fix might be a little overkill, so I offer a simpler approach at the bottom of this post&#8230;and since this post still contains some good info on dynamically mapping classes at runtime leveraging the registerClassAlias() method, I decided to leave it as is sans this simple note.</p>
<p>Say you&#8217;re using <a href="http://www.springactionscript.org/" target="_blank">Spring ActionScript</a> (SAS) or <a href="http://code.google.com/p/swizframework/" target="_blank">Swiz</a> or some other ActionScript framework for <a href="http://www.ericfeminella.com/blog/2008/09/21/dependency-injection-iocdi-in-flex/" target="_blank">Dependency Injection</a> (DI) in conjunction with the RemoteObject&#8230;in most cases you&#8217;d like to map custom ActionScript Request and Response objects to a corresponding server-side Request and Response  object and you typically do so with a small bit of metadata in your AS class &#8212; just for an example, let&#8217;s say we have a Java LoginService with the following method signature:</p>
<pre class="brush: java;">
public LoginResponseDTO login(LoginRequestDTO request) throws LoginException
</pre>
<p>And the Java LoginRequestDTO looks like:</p>
<pre class="brush: java;">
package com.domain.myproj.dto.request;

public class LoginRequestDTO
{
	public String username;
	public String password;
}
</pre>
<p>Then we&#8217;d most likely have a corresponding LoginRequestDTO in ActionScript with the following class definition:</p>
<pre class="brush: as3;">
package test
{
	[RemoteClass(alias=&quot;com.domain.myproj.dto.request.LoginRequestDTO&quot;)]
	public class LoginRequestDTO extends test.AbstractRequestDTO
	{
		public var username:String;
		public var password:String;
	}
}
</pre>
<p>And via the magic of the client-side Flash Player and the server-side AMF Remoting Gateway in BlazeDS / LCDS / WebORB / GraniteDS, our ActionScript Request and Response Objects are serialized and deserialzied into their Java counterparts and all is well&#8230;<strong>the thing is, this is really only done with some big help from the Flex compiler and the all important RemoteClass metadata</strong>&#8230;ok, so what?&#8230;let&#8217;s do some further questioning and hypothesizing&#8230;</p>
<p>Well, let&#8217;s say you&#8217;re injecting your Business Delegates or whatever objects you&#8217;re using to actually invoke the server-side LoginService, where you&#8217;re also leveraging your cool, custom AS Req/Resp objects&#8230;well if you&#8217;re injecting your BDs, then you&#8217;re kind of injecting these other objects too, which means the compiler never actually sees the RemoteClass metadata in your Req/Resp objects&#8230;now this is very important, so I&#8217;ll repeat it in a slightly different way and with more definition to it&#8217;s importantce:</p>
<p><strong>If the compiler doesn&#8217;t actually know about or see the RemoteClass metadata, then not only will the mapping of your custom AS Req/Resp objects that are supposed to map to custom Java Req/Resp objects not happen, but even worse is that core AS Remoting objects that map to their corresponding core Java Remoting Objects (part of the very framework that makes the RemoteObject possible) does not take place.</strong></p>
<p>Go ahead&#8230;create an example and you&#8217;ll get a runtime error probably saying something like: </p>
<p><code>TypeError: Error #1034: Type Coercion failed: cannot convert Object@13e9921 to mx.messaging.messages.ErrorMessage.</code></p>
<p>So now you have 2 issues with the latter being the first priority, b/c if you don&#8217;t fix that then you don&#8217;t have to even worry about the custom mappings&#8230;head in hands&#8230;what to do? Well, I know Flash guys or doods writing in pure AS3 without the Flex Framework also run into issues when trying to leverage the RemoteObject, so I googled around a bit and found the answer with the registerClassAlias() method.</p>
<p>What we need to do is map each of the core AS Objects to the core Java Objects that are required for Java-RPC &#8212; http://forums.adobe.com/thread/295996</p>
<p>The registerClassAlias method allows developers to map client-side AS objects to server-side objects at runtime so the Flash Player can create the correct mappings&#8230;so to fix our issues we need to register the core objects and our custom objects.</p>
<p>I decided to wrap this in 2 objects: 1 for the framework mappings and a 2nd for the custom mappings:</p>
<p><strong>RegisterDataServicesClassAliases</strong></p>
<pre class="brush: as3;">/**
 * Web App Solution Confidential Information
 * Copyright 2009, Web App Solution
 *
 * @author Brian Riley
 * @date July, 6, 2009
 */
package com.webappsolution.roserviceinspector.util
{
	import com.webappsolution.springactionscript.util.IRegisterRemoteObjectClassAliases;
	import com.webappsolution.springactionscript.util.RegisterRemoteObjectClassAliases;

	public class RegisterDataServicesClassAliases extends RegisterRemoteObjectClassAliases implements IRegisterRemoteObjectClassAliases
	{
		/**
		 * Register all the custom request and response objects for the application.
		 */
		override public function registerCustomFlexDataServicesImpl():void
		{
			// REQUESTS
			//
			registerClassAlias(&quot;com.domain.myproj.dto.request.LoginRequestDTO&quot;, com.domain.myproj.dto.request.LoginRequestDTO);

			// RESPONSES
			//
		}
	}
}</pre>
<p><strong>RegisterRemoteObjectClassAliases</strong></p>
<pre class="brush: as3;">/**
 * Web App Solution Confidential Information
 * Copyright 2009, Web App Solution
 *
 * @author Brian Riley
 * @date July, 29, 2009
 */
package com.webappsolution.springactionscript.util
{
	import flash.net.registerClassAlias;

	import mx.collections.ArrayCollection;
	import mx.messaging.config.ConfigMap;
	import mx.messaging.messages.*;
	import mx.utils.ObjectProxy;

	/**
	 * This little gem maps client-side ActionScript objects to their servber-side Java counterparts
	 * at runtime.
	 *
	 * &lt;p&gt;
	 * Normally this is done via the metatdata tag for RemoteObjects [RemoteClass(alias=&quot;com.domain.project.MyDTO&quot;)]
	 * and the compiler automatically creates the base AS to Java class mappings for the Flex Data Services in
	 * LCDS / BlazeDS / WebORB / GraniteDS...
	 * &lt;/p&gt;
	 *
	 * &lt;p&gt;
	 * However, there are cases where developers either don't create the metadata or inject these DTOs
	 * at runtime and thus the compiler never actually sees the metadata, so we're left to do the class
	 * mappings at runtime.
	 * &lt;/p&gt;
	 *
	 * &lt;p&gt;
	 * This class provides developers with a couple options: The developer can choose to map all the Flex
	 * Data Service components with the &lt;code&gt;public static registerFlexDataServices()&lt;/code&gt; method or
	 * they can simply call the &lt;code&gt;public static registerAll()&lt;/code&gt; method; they can also put a list
	 * of custom mappings for custom requerst and response objects in the
	 * &lt;code&gt;public static registerCustomFlexDataServices()&lt;/code&gt; method.
	 * &lt;/p&gt;
	 */
	public class RegisterRemoteObjectClassAliases
	{
		/**
		 * Constructor
		 */
		public function RegisterRemoteObjectClassAliases()
		{

		}

		/**
		 *
		 */
		public function registerAll():void
		{
			registerFlexDataServices();
			registerFlexDataServicesMisc();
			registerCustomFlexDataServices();
		}

		/**
		 * Registers the base Flex Data Service Java reguest and response objects to the base
		 * ActionScript reguest and response objects.
		 */
		public function registerFlexDataServices():void
		{
			/*
			http://forums.adobe.com/thread/295996

			I think one problem is that the various mx.messaging.messages.Message
			implementations have not called registerClassAlias() for each
			of their types. In Flex, the compiler notices the [RemoteClass] metadata
			on each ActionScript class and codegens static initializer code to call
			registerClassAlias() while the application is initializing (so
			that it occurs before any data is sent or received).

			Note you can see this generated code if you specify
			-keep-generated-actionscript=true on the command line arguments for
			mxmlc. For compiling a file called test.mxml, find the
			/generated/_test_FlexInit-generated.as file and notice the calls to
			registerClassAlias.
			*/

			// See http://www.mail-archive.com/flexcoders@yahoogroups.com/msg74512.html
			// For explanation as to why this is needed. Without it, messages were not getting mapped correctly
			// from server. 

			registerClassAlias(&quot;flex.messaging.messages.RemotingMessage&quot;, RemotingMessage);
			registerClassAlias(&quot;flex.messaging.messages.ErrorMessage&quot;, ErrorMessage);
			registerClassAlias(&quot;flex.messaging.messages.CommandMessage&quot;,CommandMessage);
			registerClassAlias(&quot;flex.messaging.messages.AcknowledgeMessage&quot;, AcknowledgeMessage);
//			registerClassAlias(&quot;flex.messaging.io.ArrayList&quot;, ArrayList);
			registerClassAlias(&quot;flex.messaging.config.ConfigMap&quot;, ConfigMap);
			registerClassAlias(&quot;flex.messaging.io.ArrayCollection&quot;, ArrayCollection);
			registerClassAlias(&quot;flex.messaging.io.ObjectProxy&quot;, ObjectProxy);

			// You may want to register pub/sub and other rpc message types too...
			registerClassAlias(&quot;flex.messaging.messages.HTTPMessage&quot;, HTTPRequestMessage);
			registerClassAlias(&quot;flex.messaging.messages.SOAPMessage&quot;, SOAPMessage);
			registerClassAlias(&quot;flex.messaging.messages.AsyncMessage&quot;, AsyncMessage);
			registerClassAlias(&quot;flex.messaging.messages.MessagePerformance Info&quot;, MessagePerformanceInfo);
		}

		/**
		 * Additional message types...doesn't work in Flex 2.0.1 SDK, so comment out the
		 * the body of the method if that's the case.
		 */
		public function registerFlexDataServicesMisc():void
		{
//			registerClassAlias(&quot;DSA&quot;, AsyncMessageExt); // doesn't work in Flex 2.0.1 SDK
//			registerClassAlias(&quot;DSC&quot;, CommandMessageExt); // doesn't work in Flex 2.0.1 SDK
//			registerClassAlias(&quot;DSK&quot;, AcknowledgeMessageExt); // doesn't work in Flex 2.0.1 SDK
		}

		/**
		 * Registers the custom Java reguest and response objects to the custom ActionScript reguest and response objects.
		 * Developers should list their custom objects here.
		 */
		public function registerCustomFlexDataServices():void
		{
			// map the Java Data Transfer Object passed over HTTP back to Flex to the AS object
			registerCustomFlexDataServicesImpl();
		}

		/**
		 * Concrete classes must override this method, as this is just a hook operation.
		 */
		public function registerCustomFlexDataServicesImpl():void
		{
			// concrete classes must override this bad boy
		}

	}
}</pre>
<p>And then I simply call the following somewhere in my app&#8217;s initialization process (very early on):</p>
<pre class="brush: as3;">
private function registerDataServices():void
{
	logger.debug(&quot;registerDataServices&quot;);

	var registerROAliases:RegisterDataServicesClassAliases;

	// since we're injecting classes at runtime we need to set up the registerClassAlias() to
	// Flex Data Service classes manually
	registerROAliases = new RegisterDataServicesClassAliases();
	registerROAliases.registerAll();
}
</pre>
<p>And voila! We&#8217;re back in business. </p>
<p>I&#8217;d like to thank my local, Boston Adobe Flex guru for providing the basic solution to my issue in the following link: <a href="http://forums.adobe.com/thread/295996" target="_blank">http://forums.adobe.com/thread/295996</a></p>
<p><strong>UPDATED FIX:</strong></p>
<p>So I&#8217;m an idiot or rather someone that tends to overarchitect/think things sometimes&#8230;everywhere else in my app I simply make sure my classes injected at runtime are compiled into the app by simply referencing them somewhere else in the app or by putting them into a properties file like so:</p>
<p><code>LoginRequestDTO	= ClassReference("com.domain.myproj.dto.request.LoginRequestDTO")</code></p>
<p>This will force the compilation of the LoginRequestDTO and thus allow the compiler to see the RemoteClass metadata&#8230;but at least you know more about what the metadata does and how it works in conjunction with the Flex compiler <img src='http://www.webappsolution.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=ActionScript+Dependency+Injection+With+RemoteClass+Metadata+Issue+With+Fix+-+UPDATED+http://3gc86.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.webappsolution.com/wordpress/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=ActionScript+Dependency+Injection+With+RemoteClass+Metadata+Issue+With+Fix+-+UPDATED+http://3gc86.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webappsolution.com/wordpress/2009/08/31/actionscript-dependency-injection-with-remoteclass-metadata-issue-with-fix/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Flex + Cairngorm + Spring ActionScript Part 5 Announcement</title>
		<link>http://www.webappsolution.com/wordpress/2009/06/26/flex-cairngorm-spring-actionscript-part-5-announcement/</link>
		<comments>http://www.webappsolution.com/wordpress/2009/06/26/flex-cairngorm-spring-actionscript-part-5-announcement/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 21:41:33 +0000</pubDate>
		<dc:creator>brianr</dc:creator>
		
		<category><![CDATA[cairngorm]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[flex]]></category>

		<category><![CDATA[spring-actionscript]]></category>

		<guid isPermaLink="false">http://www.webappsolution.com/wordpress/?p=392</guid>
		<description><![CDATA[

UPDATE: I&#8217;ve decided not to continue this tutorial at the moment as I&#8217;m really digging the lightweight, simple elegance, and less complicated Swiz framework. I will finish the path of this original tutorial and dig into BlazeDS + Spring + etc, but with the obvious replacement of Swiz over SAS + CG. Sorry for the [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<div id="commentbody-113">
<p><strong>UPDATE</strong>: I&#8217;ve decided not to continue this tutorial at the moment as I&#8217;m really digging the lightweight, simple elegance, and less complicated Swiz framework. I will finish the path of this original tutorial and dig into BlazeDS + Spring + etc, but with the obvious replacement of Swiz over SAS + CG. Sorry for the confusion and delay.</p>
<p>I’m going to release Part 5 of the series shortly, although it’s taking a slight deviation from what I had planned originally…</p>
<p>The Spring AS (”SAS”) framework has been changing quite a bit and they’ve released new versions and <a rel="nofollow" href="http://www.springactionscript.org/docs/reference/html/springactionscript.html">new docs for both the framework itself and the Cairngorm (”CG”) extensions</a>, so I’d like to revisit my SAS + CG implementation leveraging their approach.</p>
<p>I’ve also added in a login screen to set up the use of Spring Security with role based permissions on the Java side — it also allows me to illustrate the use of multiple Cairngorm Events + Command + Delegate paths with both hardcoded AS and XML Delegates.</p>
<p>The actual code is done, now I just have to write about it…I&#8217;m about 3/4 of the way through the actual post explaining the code, but it&#8217;s Friday and I need a beer so here&#8217;s the code to tide those waiting on it over. Again, the full blog post with details explaining the ins and outs to come some time next wk.</p>
<p><strong>Assets</strong>:</p>
<ul>
<li><a title="Employee Management Console 5" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-5/EmployeeManagementConsole5.html" target="_blank">Demo Application</a><a title="Project View Source" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/srcview/index.html" target="_blank"><br />
</a></li>
<li><a title="View Source" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-5/srcview/index.html" target="_blank">View Source<br />
</a></li>
</ul>
<p><strong>Previous Tutorials:</strong></p>
<ul>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1: Basic Flex + CG Application</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2: Flex + CG + SAS</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/" target="_blank">Part 3: Flex + CG + SAS + Injecting Services into Business Delegates</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/06/11/flex-cairngorm-spring-actionscript-tomcat-weborbblazeds-spring-java-hibernate-mysql-tutorial-part-4/" target="_blank">Part 4: Integrated Flex Project + Java Project with Tomcat</a></li>
</ul>
<p>Stay tuned for the real Part 5 in the series.</p>
</div>
</div>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+Part+5+Announcement+http://ppif2.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.webappsolution.com/wordpress/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+Part+5+Announcement+http://ppif2.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webappsolution.com/wordpress/2009/06/26/flex-cairngorm-spring-actionscript-part-5-announcement/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Flex + Cairngorm + Spring ActionScript + Tomcat + WebORB/BlazeDS + Spring Java + Hibernate + MySQL Tutorial Part 3</title>
		<link>http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/</link>
		<comments>http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/#comments</comments>
		<pubDate>Thu, 14 May 2009 14:35:46 +0000</pubDate>
		<dc:creator>brianr</dc:creator>
		
		<category><![CDATA[cairngorm]]></category>

		<category><![CDATA[flex]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webappsolution.com/wordpress/?p=251</guid>
		<description><![CDATA[Introduction
I&#8217;ve been playing around with a stack of Flex/ActionScript and Java frameworks and finally came up with one that I&#8217;m really pleased with &#8212; since I&#8217;m reusing these terms throughout the series, please review the acronyms after each, as that&#8217;s how I&#8217;ll be referring to them in the tutorial:

Flex
Cairngorm (&#8221;CG&#8221;)

Spring ActionScript (&#8221;SAS&#8221;), formerly Prana
Tomcat (&#8221;TC&#8221;)

WebORB [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>I&#8217;ve been playing around with a stack of Flex/ActionScript and Java frameworks and finally came up with one that I&#8217;m really pleased with &#8212; since I&#8217;m reusing these terms throughout the series, please review the acronyms after each, as that&#8217;s how I&#8217;ll be referring to them in the tutorial:</p>
<ul>
<li><a title="Flex" href="http://www.adobe.com/products/flex/" target="_blank">Flex</a></li>
<li><a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a> (&#8221;CG&#8221;)<a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank"><br />
</a></li>
<li><a title="Spring ActionScript Framework" href="http://www.springsource.org/extensions/se-springactionscript-as" target="_blank">Spring ActionScript</a> (&#8221;SAS&#8221;), formerly <a title="Prana" href="http://www.pranaframework.org/" target="_blank">Prana</a></li>
<li><a title="Tomcat" href="http://tomcat.apache.org/" target="_blank">Tomcat</a> (&#8221;TC&#8221;)<a title="Tomcat" href="http://tomcat.apache.org/" target="_blank"><br />
</a></li>
<li><a title="WebORB for Java" href="http://www.themidnightcoders.com/products/weborb-for-java" target="_blank">WebORB</a> (&#8221;WORB&#8221;) /<a title="BlazeDS" href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/" target="_blank"> BlazeDS</a> (&#8221;BDS&#8221;)<a title="BlazeDS" href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/" target="_blank"><br />
</a></li>
<li><a title="Spring" href="http://www.springsource.com" target="_blank">Spring</a>, Server-side Java version</li>
<li><a title="Hibernate" href="https://www.hibernate.org/" target="_blank">Hibernate</a> (&#8221;HB&#8221;)<a title="Hibernate" href="https://www.hibernate.org/" target="_blank"><br />
</a></li>
<li><a title="MySQL" href="http://www.mysql.com/" target="_blank">MySQL</a></li>
</ul>
<p>To that, I&#8217;m planing on writing a series of tutorials where each one builds on the previous one. The final tutorial will cover: <strong>Flex + Cairngorm + Spring ActionScript + Tomcat + WebORB/BlazeDS + Spring Java + Spring Security + Hibernate + MySQL</strong></p>
<ul>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1: Basic Flex + CG Application</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2: Flex + CG + SAS</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/" target="_blank">Part 3: Flex + CG + SAS + Injecting Services into Business Delegates</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/06/11/flex-cairngorm-spring-actionscript-tomcat-weborbblazeds-spring-java-hibernate-mysql-tutorial-part-4/" target="_blank">Part 4: Integrated Flex Project + Java Project with Tomcat</a></li>
</ul>
<p>Part 3 in our series will build on our working knowledge of  <a title="Spring ActionScript Framework" href="http://www.springsource.org/extensions/se-springactionscript-as" target="_blank">Spring ActionScript framework</a> (&#8221;SAS&#8221;) and use <a title="Dependency Injection" href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency Injection</a> (&#8221;DI&#8221;) to add the services to our Business Delegates (&#8221;BD&#8221;) from <a title="Part 2: Flex + SAS + Caringorm" href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2</a>.</p>
<p><strong><span style="color: #008000;">Tutorial Goal</span></strong><strong>: Inject Service Definitions into Business Delegates using Spring AS<br />
</strong></p>
<p>Build upon the existing, foundational Flex + <a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a> (&#8221;CG&#8221;)  + SAS application and inject the desired services into our Business Delegate (&#8221;BD&#8221;) at runtime; we&#8217;ll continue working with the EmployeeXMLDelegate and inject the necessary HTTPservice and it&#8217;s properties as opposed to hardcoding them directly in the BD. After this tutorial you should know why and how to inject complex objects with SAS.</p>
<p><strong>Assets</strong>:</p>
<ul>
<li><a title="Project Demo" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/EmployeeManagementConsole3.html" target="_blank">Project Demo</a></li>
<li><a title="Project View Source" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/srcview/index.html" target="_blank">Project View Source</a></li>
<li><a title="Project Source Files" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/srcview/EmployeeManagementConsole3.zip" target="_blank">Project Source Files</a></li>
</ul>
<p><strong>Prerequisites &amp; Assumptions</strong></p>
<ul>
<li>Ability to create Flex and ActionScript classes with <a title="Flex Builder" href="http://www.adobe.com/products/flex/features/flex_builder/" target="_blank">Flex Builder</a> (&#8221;FB&#8221;). NOTE: I&#8217;m actually using <a title="Eclipe WTP" href="http://www.eclipse.org/webtools/" target="_blank">Eclipse WTP</a> with the Flex Builder Plugin so I can develop in both Java/J2EE and Flex projects in one IDE.</li>
<li>Used or understand Cairngorm&#8217;s basic flow and how it all fits together. If you haven&#8217;t, I recommend looking at <a title="Part 1: Flex + Cairngorm" href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1</a> of this tutorial and the <a title="Cairngorm Diagram" href="http://www.cairngormdocs.org/tools/CairngormDiagramExplorer.swf" target="_blank">Cairngorm Diagram</a> for some reference.</li>
<li>Basic understanding of Spring ActionScript and Dependency Injection. If you haven&#8217;t, I recommend looking at <a title="Part 2: Flex + SAS + Caringorm" href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2</a>.</li>
<li>Understanding of OOP and basic Design Patterns.</li>
</ul>
<p><strong>What This Tutorial is Not</strong></p>
<p>There&#8217;s actually a set of extensions for SAS for both CG and <a title="PureMVC" href="http://puremvc.org/" target="_blank">PureMVC</a> (&#8221;PMVC&#8221;), but since CG is more widely used and I want to keep things simple, this tutorial will focus on SAS + CG&#8230;so leave the frameworks debate and why I chose CG for this example for another time, or at least until another post where I actually want to argue the architectural issues that both frameworks possess. I&#8217;m also not going to focus on CG best-practices and extensions that <a title="Web App Solution, Inc." href="http://webappsolution.com" target="_blank">WASI</a> currently uses in this first post&#8230;let&#8217;s just get a SAS + CG app up and running and then come back to that in a later, cleanup/best-practices post.</p>
<p><strong>NOTE</strong>: Most of the code snippets are truncated to be concise and only highlight the lines of code that truly deserve the readers attention, so it&#8217;s highly recommended that readers download the accompanying <a title="Project Source Files" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/srcview/EmployeeManagementConsole3.zip" target="_blank">project source files</a> in order to follow along.</p>
<p><strong>Create a Flex Builder Project</strong></p>
<p>If you don&#8217;t already have the project from <a title="Part 2: Flex + SAS + Caringorm" href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2</a> of this series, please <a title="Download Part 2" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/EmployeeManagementConsole2.zip" target="_blank">download it</a> and import it into Flex Builder (&#8221;FB&#8221;). Once in FB, copy and paste it into the same workspace as &#8220;EmployeeManagementConsole2&#8243;.</p>
<p><strong>NOTE</strong>: If you don&#8217;t feel like writing all this from scratch, just <a title="Download the Project Source Files" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-3/srcview/EmployeeManagementConsole3.zip" target="_blank">download my project for Part 3</a>.</p>
<p><strong>Remove Hardcoded Service From EmployeeXMLDelegate<br />
</strong></p>
<p>Open up EmployeeXMLDelegate and <strong>remove</strong> the class property <strong>httpService</strong> and it&#8217;s instantiation from the constructor. You&#8217;re BD&#8217;s constructor should now look like this:</p>
<p><strong></strong></p>
<pre><code>public class EmployeeXMLDelegate extends AbstractDelegate implements IEmployeeDelegate
{
	/**
	 * Constructor.
	 */
	public function EmployeeXMLDelegate(responder:IResponder=null)
	{
		trace("EmployeeXMLDelegate Constructor");

		this.responder = responder;
	}
        ...
}
</code></pre>
<p><strong>Add HTTPService Service to AbstractDelegate<br />
</strong></p>
<p>Open up AbstractDelegate and <strong>add</strong> the class property <strong>httpService</strong>. You&#8217;re BD&#8217;s constructor should now look like this:</p>
<pre><code>public class AbstractDelegate
{
	/**
	 * Reference to the XML / HTTP service the concrete delegate's will will use.
	 */
	public var httpService:HTTPService;

	/**
	 * Constructor.
	 */
	public function AbstractDelegate()
	{
		trace("AbstractDelegate Constructor");
	}
        ...
</code></pre>
<p>Notice that the <strong>httpService</strong> property was given the <strong>public</strong> modifier so that we can actually perform the DI &#8212; if it&#8217;s private or protected we can&#8217;t inject the HTTPService.</p>
<p><strong>Modify the Spring ApplicationContext &amp; Inject Services</strong></p>
<p>Open the SAS Application Context (&#8221;SASAC&#8221;) file and locate the definition of the EmployeeXMLDelegate in the node <strong>&lt;property name=&#8221;employeeDelegate&#8221;&gt;</strong>; here&#8217;s where we&#8217;re looking to inject our HTTPService as a property of our BD. Notice the <strong>&lt;property name=&#8221;httpService&#8221;&gt;</strong> &#8211;  it matches the exact property name of the <strong>httpService</strong> that we created in our AbstractDelegate and is where we&#8217;ll perform the DI. Just like the previously hardcoded version of our EmployeeXMLDelegate, we&#8217;ll set the following properties on our HTTPService:</p>
<ul>
<li>url = ../assets/xml/employee_list.xml</li>
<li>resultFormat = e4x</li>
<li>method = GET</li>
</ul>
<p>You&#8217;re SASAC file should now look somewhat like the following snippet. <strong>Note</strong>: I removed extraneous nodes (ie the DelegateLocator, etc) from this snippet to be concise; <a title="Download Part 3" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/EmployeeManagementConsole2.zip" target="_blank">please review the accompanying files available for download</a> or see the the project from <a title="Part 2: Flex + SAS + Caringorm" href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2</a> for more details.</p>
<p><strong>ApplicationContext.xml</strong></p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;objects&gt;
...
&lt;property name="employeeDelegate"&gt;
	&lt;object
	   class="com.wasi.employeeconsole.business.delegates.EmployeeXMLDelegate"&gt;
		&lt;property name="httpService"&gt;
			&lt;object class="mx.rpc.http.HTTPService"&gt;
				&lt;property
					name="url"
					value="../assets/xml/employee_list.xml" /&gt;
				&lt;property
					name="resultFormat"
					value="e4x" /&gt;
				&lt;property
					name="method"
					value="GET" /&gt;
			&lt;/object&gt;
		&lt;/property&gt;
	&lt;/object&gt;
&lt;/property&gt;
...
&lt;/objects&gt;
</code></pre>
<p><strong>If you run the application now you&#8217;ll receive an error</strong> &#8212; this is because we haven&#8217;t included the HTTPService anywhere in the application and yet we&#8217;re trying to instantiate it at runtime. <a title="SAS Error" href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/#class-ref-error" target="_blank">Since we ran into this issue in Part 2, I won&#8217;t go into details and you can read about it here</a> &#8212; it refers to an issue when trying to inject a BD, but the issue is the same here with the HTTPService. What we need to do to fix the issue is to add a reference to the HTTPService in the ClassReferences.properties file like we did for the Delegates we injected.</p>
<p><strong>ClassReferences.properties</strong></p>
<p>Add the following to the ClassReferences file:</p>
<pre><code>HTTPService = ClassReference("mx.rpc.http.HTTPService")</code></pre>
<p><strong>Compile &amp; Run the Application</strong></p>
<p>When you test your application you should see something like:</p>
<div class="wp-caption alignnone" style="width: 552px"><img title="Employee Mgmt Console with XML Business Delegate" src="http://www.webappsolution.com/images/blog/emp-mgmt-console-xml-data-view.png" alt="Employee Mgmt Console with XML Business Delegate" width="542" height="368" /><p class="wp-caption-text">Employee Management Console with XML Business Delegate with HTTPService Injected</p></div>
<p>So now we&#8217;re back to a working application, but we&#8217;ve injected the HTTPService into our BD making it even more flexible from a configuration standpoint. If you&#8217;d like to explore this further try one of the following or both:</p>
<ol>
<li>Move the <strong>employee_list.xml</strong> file to somewhere else in the project or file system and simply change the <strong>url</strong> property of the <strong>httpService</strong> in the Spring AS ApplicationContext file &#8212; notice that you don&#8217;t have to recompile your Flex app either, as the SASAC file is loaded at runtime.</li>
<li>Create a Java servlet (or .NET or PHP script) that generates the same XML structure as the <strong>employee_list.xml </strong>and change the <strong>url</strong> property of the <strong>httpService</strong> in the Spring AS ApplicationContext file to point to that dynamic XML service provider.</li>
</ol>
<p><strong>References</strong>:</p>
<ul>
<li><a title="Allen Manning's Spring + Cairngorm Tutorial" href="http://www.allenmanning.com/?p=25" target="_blank">Dependency Injection in Flex Applications - Part 1 - Spring ActionScript and Cairngorm</a> - Allen Manning</li>
<li><a title="IoC and the Dependency Injection Pattern in Flex" href="http://www.ericfeminella.com/blog/2008/09/21/dependency-injection-iocdi-in-flex/" target="_blank">IoC and the Dependency Injection Pattern in Flex</a> - Eric Feminella</li>
<li><a href="http://www.herrodius.com/blog/" target="_blank">Christophe Herreman&#8217;s Blog</a> - Christophe Herreman</li>
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+%2B+Tomcat+%2B+WebORB%2FBlazeDS+%2B+Spring+Java+%2B+Hibernate+%2B+MySQL+Tutorial+Part+3+http://4krcm.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.webappsolution.com/wordpress/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+%2B+Tomcat+%2B+WebORB%2FBlazeDS+%2B+Spring+Java+%2B+Hibernate+%2B+MySQL+Tutorial+Part+3+http://4krcm.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Flex + Cairngorm + Spring ActionScript + Tomcat + WebORB/BlazeDS + Spring Java + Hibernate + MySQL Tutorial Part 2</title>
		<link>http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/</link>
		<comments>http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/#comments</comments>
		<pubDate>Fri, 08 May 2009 20:33:02 +0000</pubDate>
		<dc:creator>brianr</dc:creator>
		
		<category><![CDATA[cairngorm]]></category>

		<category><![CDATA[flex]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webappsolution.com/wordpress/?p=100</guid>
		<description><![CDATA[Introduction
I&#8217;ve been playing around with a stack of Flex/ActionScript and Java frameworks and finally came up with one that I&#8217;m really pleased with &#8212; since I&#8217;m reusing these terms throughout the series, please review the acronyms after each, as that&#8217;s how I&#8217;ll be referring to them in the tutorial:

Flex
Cairngorm (&#8221;CG&#8221;)

Spring ActionScript (&#8221;SAS&#8221;), formerly Prana
Tomcat (&#8221;TC&#8221;)

WebORB [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>I&#8217;ve been playing around with a stack of Flex/ActionScript and Java frameworks and finally came up with one that I&#8217;m really pleased with &#8212; since I&#8217;m reusing these terms throughout the series, please review the acronyms after each, as that&#8217;s how I&#8217;ll be referring to them in the tutorial:</p>
<ul>
<li><a title="Flex" href="http://www.adobe.com/products/flex/" target="_blank">Flex</a></li>
<li><a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a> (&#8221;CG&#8221;)<a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank"><br />
</a></li>
<li><a title="Spring ActionScript Framework" href="http://www.springsource.org/extensions/se-springactionscript-as" target="_blank">Spring ActionScript</a> (&#8221;SAS&#8221;), formerly <a title="Prana" href="http://www.pranaframework.org/" target="_blank">Prana</a></li>
<li><a title="Tomcat" href="http://tomcat.apache.org/" target="_blank">Tomcat</a> (&#8221;TC&#8221;)<a title="Tomcat" href="http://tomcat.apache.org/" target="_blank"><br />
</a></li>
<li><a title="WebORB for Java" href="http://www.themidnightcoders.com/products/weborb-for-java" target="_blank">WebORB</a> (&#8221;WORB&#8221;) /<a title="BlazeDS" href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/" target="_blank"> BlazeDS</a> (&#8221;BDS&#8221;)<a title="BlazeDS" href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/" target="_blank"><br />
</a></li>
<li><a title="Spring" href="http://www.springsource.com" target="_blank">Spring</a>, Server-side Java version</li>
<li><a title="Hibernate" href="https://www.hibernate.org/" target="_blank">Hibernate</a> (&#8221;HB&#8221;)<a title="Hibernate" href="https://www.hibernate.org/" target="_blank"><br />
</a></li>
<li><a title="MySQL" href="http://www.mysql.com/" target="_blank">MySQL</a></li>
</ul>
<p>To that, I&#8217;m planing on writing a series of tutorials where each one builds on the previous one. The final tutorial will cover: <strong>Flex + Cairngorm + Spring ActionScript + Tomcat + WebORB/BlazeDS + Spring Java + Spring Security + Hibernate + MySQL</strong></p>
<ul>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1: Basic Flex + CG Application</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/" target="_blank">Part 2: Flex + CG + SAS</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/05/14/flex-cairngorm-spring-actionscript-tomcat-blazeds-spring-java-hibernate-mysql-tutorial-part-3/" target="_blank">Part 3: Flex + CG + SAS + Injecting Services into Business Delegates</a></li>
<li><a href="http://www.webappsolution.com/wordpress/2009/06/11/flex-cairngorm-spring-actionscript-tomcat-weborbblazeds-spring-java-hibernate-mysql-tutorial-part-4/" target="_blank">Part 4: Integrated Flex Project + Java Project with Tomcat</a></li>
</ul>
<p>Part 2 in our series will focus on the introduction of  the <a title="Spring ActionScript Framework" href="http://www.springsource.org/extensions/se-springactionscript-as" target="_blank">Spring ActionScript framework</a> (&#8221;SAS&#8221;) to our existing Flex + <a title="Cairngorm" href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a> (&#8221;CG&#8221;) application from <a title="Part 1: Flex + Cairngorm" href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1</a>.</p>
<p><strong><span style="color: #008000;">Tutorial Goal</span></strong><strong>: Flex + Cairngorm + Spring ActionScript<br />
</strong></p>
<p>Build upon the existing, foundational Flex + CG application by adding SAS and injecting the desired CG Business Delegate (&#8221;BD&#8221;) at runtime; we&#8217;ll continue working with the mock-object EmployeeASDelegate, but also add a XML based BD called Employee<em><strong>XML</strong></em>Delegate so we can choose our data provider at runtime. After this tutorial you should know why and how to use SAS with CG.</p>
<p><strong>Assets</strong>:</p>
<ul>
<li><a title="Project Demo" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/EmployeeManagementConsole2.html" target="_blank">Project Demo</a></li>
<li><a title="Project View Source" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/index.html" target="_blank">Project View Source</a></li>
<li><a title="Project Source Files" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/EmployeeManagementConsole2.zip" target="_blank">Project Source Files</a></li>
</ul>
<p><strong>Prerequisites &amp; Assumptions</strong></p>
<ul>
<li>Ability to create Flex and ActionScript classes with <a title="Flex Builder" href="http://www.adobe.com/products/flex/features/flex_builder/" target="_blank">Flex Builder</a> (&#8221;FB&#8221;). NOTE: I&#8217;m actually using <a title="Eclipe WTP" href="http://www.eclipse.org/webtools/" target="_blank">Eclipse WTP</a> with the Flex Builder Plugin so I can develop in both Java/J2EE and Flex projects in one IDE.</li>
<li>Used or understand Cairngorm&#8217;s basic flow and how it all fits together. If you haven&#8217;t, I recommend looking at <a title="Part 1: Flex + Cairngorm" href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1</a> of this tutorial and the <a title="Cairngorm Diagram" href="http://www.cairngormdocs.org/tools/CairngormDiagramExplorer.swf" target="_blank">Cairngorm Diagram</a> for some reference.</li>
<li>Understanding of OOP and basic Design Patterns.</li>
<li><strong>NOTE</strong>: Most of the code snippets are truncated to be concise and only highlight the lines of code that truly deserve the readers attention, so it&#8217;s highly recommended that readers download the accompanying <a title="Project Source Files" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/EmployeeManagementConsole2.zip" target="_blank">project source files</a> in order to follow along.</li>
</ul>
<p><strong>What This Tutorial is Not</strong></p>
<p>There&#8217;s actually a set of extensions for SAS for both CG and <a title="PureMVC" href="http://puremvc.org/" target="_blank">PureMVC</a> (&#8221;PMVC&#8221;), but since CG is more widely used and I want to keep things simple, this tutorial will focus on SAS + CG&#8230;so leave the frameworks debate and why I chose CG for this example for another time, or at least until another post where I actually want to argue the architectural issues that both frameworks possess.  I&#8217;m also not going to focus on CG best-practices and extensions that WASI currently uses in this first post&#8230;let&#8217;s just get a SAS + CG app up and running and then come back to that in a later, cleanup/best-practices post.  <strong></strong></p>
<p><strong>So without any further delay, why Spring ActionScript (&#8221;SAS&#8221;)?</strong></p>
<p>Ever create an app that relies on data from a DB that isn&#8217;t created or populated yet or try to work with services that don&#8217;t return any data? Sure you have, and so we all have our own frameworks and tricks for setting up mock objects in Business Delegates (&#8221;BD&#8221;) or simply consume XML until the real data is available. And that works great for awhile. You sit down with the server-side guys and flesh out your service API, the inputs and outputs, and hardcode your data in this temporary fashion until you hit the phase of your project called &#8220;integration.&#8221; What a lovely word. I&#8217;ve spent months of fun playing in the &#8220;integration phase&#8221; where you actually hook up your app to live data services, and you spend a good deal of time just ripping out scaffolding code, those hard-coded data calls, and&#8230;bleh&#8230;what a mess. So what if there was a way to simply interchange the BDs with their hard-coded mock objects, your stubbed service calls, or your XML through a simple config file? This my friends is the beauty of SAS and is why you&#8217;ll be hooked the moment you light up your first app with it.</p>
<p>The idea is to decouple configuration from implementation in patterns known as <a title="Inversion of Control" href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">Inversion of Control</a> (&#8221;IoC&#8221;) and <a title="Dependency Injection" href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency Injection</a> (&#8221;DI&#8221;) which are essentially the heart of Spring. Rather than go into too much detail about either of these patterns, <a title="Eric Feminella's IoC &amp; DI Post" href="http://www.ericfeminella.com/blog/2008/09/21/dependency-injection-iocdi-in-flex/" target="_blank">please read Eric Feminellas great post on the topic</a>. Furthermore, this is being very narrow minded and I&#8217;m leaving out a ton of other great things that Spring does, but this is the meat and potatoes of it and is what we&#8217;ll focus on in our first SAS tutorial.</p>
<p>A huge thanks to <a title="Christophe Herreman" href="http://www.herrodius.com/blog/" target="_blank">Christophe Herreman</a> for porting this beauteous code-base over from Java to AS! SAS, formerly <a title="Prana" href="http://www.pranaframework.org/" target="_blank">Prana</a>, is now being considered as part of the original <a title="Java SpringSource Community" href="http://www.springsource.com/" target="_blank">Java SpringSource Community</a>&#8217;s code repository.  Now that we have an idea of how SAS can help us, let&#8217;s see it in practice.  <strong></strong> <strong></strong></p>
<p><strong>Create a Flex Builder Project</strong></p>
<p>If you don&#8217;t already have the project from <a title="Part 1: Flex + Cairngorm" href="http://www.webappsolution.com/wordpress/2009/05/01/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt1/" target="_blank">Part 1</a> of this series, please <a title="Downlaod Part 1" href="http://www.webappsolution.com/flex/blog/examples/emp-mgmt-console-2/srcview/EmployeeManagementConsole2.zip" target="_blank">download it</a> and import it into Flex Builder. Once in FB, copy and paste it into the same workspace as &#8220;EmployeeManagementConsole2&#8243;.</p>
<p><strong>Add Libraries and Dependencies </strong></p>
<p>Next we&#8217;ll need to add the necessary libraries and dependencies to the project as SWCs and AS files; mainly, we&#8217;ll need:</p>
<p><strong><a title="Spring AS Download" href="http://sourceforge.net/project/showfiles.php?group_id=194107&amp;package_id=306949" target="_blank">Spring ActionScript</a></strong> &#8212; Download the ZIP, extract it, and import the spring-actionscript.swc into the libs folder of your Flex Builder project.   <strong><a title="SAS Extensions Download" href="https://src.springframework.org/svn/se-springactionscript-as" target="_blank"></a></strong></p>
<p><strong><a title="SAS Extensions Download" href="https://src.springframework.org/svn/se-springactionscript-as" target="_blank">Spring ActionScript Cairngorm Extensions</a></strong> &#8212; I couldn&#8217;t find the SWC for this, so I just downloaded the AS source by using Subversion (I&#8217;m currently using <a title="Subclipse" href="http://subclipse.tigris.org/servlets/ProjectProcess?pageID=p4wYuA" target="_blank">Subclipse</a>, the Eclipse plug-in for SVN)  and going <a title="SAS Extensions Download" href="https://src.springframework.org/svn/se-springactionscript-as" target="_blank">here &#8212; https://src.springframework.org/svn/se-springactionscript-as/</a>.  Checkout the project &#8220;spring-actionscript-cairngorm&#8221; as a Flex Library Project. Once the project has completely checked out, disconnect it from SVN &#8212; if you are using Subclipse, just right-click on the project -&gt; Team -&gt; Disconnect (and delete all the SVN contents as well.) Finally, dig into the Flex Library Project you just created and locate the following directory: trunk/core/src/main/actionscript/org. Copy the root folder for the Cairngorm Exts sourcem <strong>org</strong> and all of it&#8217;s child directories and paste it into the <strong>src</strong> folder of your original EmployeeManagementConsole2 project.  <strong></strong></p>
<p><strong>Create Simple XML Data Source File</strong></p>
<p>First we need to go ahead and create our XML data source, so let&#8217;s create a new XML file called &#8220;employee_list.xml&#8221; and save it in our project in a root folder called /assets/xml. We&#8217;ll model the XML directly after the EmployeeModel.as Model object (also like the AS mock-object we created in the AS BD), except that we&#8217;ll make the ID property an attribute in each employee node. We&#8217;ll also add &#8220;XML&#8221; as a suffix to each first name node value just so we can differentiate the data source later on when we start switching between the AS mock-object BD and our new XML BD. The finished XML should look something like this:  <strong>employee_list.xml</strong></p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;employee id="0"&gt;
		&lt;firstName&gt;BrianXML&lt;/firstName&gt;
		&lt;lastName&gt;Riley&lt;/lastName&gt;
	&lt;/employee&gt;

	&lt;employee id="1"&gt;
		&lt;firstName&gt;TimXML&lt;/firstName&gt;
		&lt;lastName&gt;McGee&lt;/lastName&gt;
	&lt;/employee&gt;

	&lt;employee id="2"&gt;
		&lt;firstName&gt;JoeXML&lt;/firstName&gt;
		&lt;lastName&gt;Seiter&lt;/lastName&gt;
	&lt;/employee&gt;

&lt;/employeeList&gt;
</code></pre>
<p><strong>Create New XML Business Delegate</strong></p>
<p>Since we already have the application running with the AS mock-object BD and we want the app to work with XML as well, we need to go ahead and create a new XML BD, Employee<em><strong>XML</strong></em>Delegate.as. The key differences between this BD and the AS BD are the following:</p>
<ul>
<li>Has its own HTTPService object to make requests for the XML Employee List data.</li>
<li>Calls the HTTPService in the getList() method.</li>
<li>Deserializes the XML result into an ArrayCollection (&#8221;AC&#8221;) of EmployeeModel objects.</li>
</ul>
<p>These changes all exist in the constructor, getList(), and result() methods.</p>
<p><strong>EmployeeXMLDelegate.as</strong></p>
<pre><code>/**
 * Constructor.
 */
public function EmployeeXMLDelegate(responder:IResponder=null)
{
	trace("EmployeeXMLDelegate Constructor");

	this.responder = responder;

	// the serives are usually created in the ServiceLocator ("SL") in
	// Cairngorm but since we're going to do away with the SL when we
	// introduce Spring AS, we'll just hardcode the service here for now.
	this.httpService = new HTTPService();
	this.httpService.url = "../assets/xml/employee_list.xml";
	this.httpService.resultFormat = "e4x";
}

/**
 * Get the list of employees.
 */
public function getList():void
{
	trace("EmployeeXMLDelegate getList");

	var responder:mx.rpc.Responder;
 	var token:AsyncToken;

 	responder = new mx.rpc.Responder(result, fault);
	token = this.httpService.send();
	token.addResponder(responder);
}

/**
 * Handles the successful service request.
 *
 * @param response Object The success event coming back from the asynchronous
 * service call containing the data payload.
 */
public function result(resultEvent:ResultEvent):void
{
	trace("EmployeeXMLDelegate result");

	var response:ArrayCollection;
	var employee:EmployeeModel;

	response = new ArrayCollection();

	for each(var employeeXML:XML in resultEvent.result.employee)
	{
		employee = new EmployeeModel();
		employee.id = employeeXML.@id;
		employee.firstName = employeeXML.firstName;
		employee.lastName = employeeXML.lastName;
		response.addItem(employee);
	}

	// pass the command the response object to do whatever it wants with it
	this.responder.result(response);
}
</code></pre>
<p>Now that we have our XML BD, let&#8217;s make some quick changes in the CMD to test it out. We&#8217;ll need to modify the reference to the BD in 2 places in the CMD:</p>
<ol>
<li>The delegate class property</li>
<li>The instantiation of that delegate in the constructor</li>
</ol>
<p><strong>GetEmployeeListCommand.as</strong></p>
<pre><code>public class GetEmployeeListCommand implements ICommand, IResponder
{
	private var delegate:EmployeeXMLDelegate;

	public function GetEmployeeListCommand()
	{
		trace("GetEmployeeListCommand Constructor");
		this.delegate = new EmployeeXMLDelegate(this);
	}
       ...
}
</code></pre>
<p>When you test your application you should see something like:</p>
<div class="wp-caption alignnone" style="width: 552px"><img title="Employee Mgmt Console with XML Business Delegate" src="http://www.webappsolution.com/images/blog/emp-mgmt-console-xml-data-view.png" alt="Employee Mgmt Console with XML Business Delegate" width="542" height="368" /><p class="wp-caption-text">Employee Management Console with XML Business Delegate</p></div>
<p><strong>Add Spring ActionScript</strong></p>
<p>Now we have 2 possible delegates to choose from, but we have to change the AS in the CMD, recompile, and then test to see the results&#8230;this sounds tedious and leaves us with the very problem we&#8217;re trying to avoid&#8230;time for SAS.</p>
<p>In order to see the changes occur at run-time we&#8217;ll need to create the Spring Application Context XML configuration file that defines which BD to inject at run-time and then modify the CMD in such a way that the exact type of BD is unknown at compile time&#8230;well not exactly an unknown type, but rather the underlying implementation. To achieve this feat we&#8217;ll leverage the almighty Interface in our CMDs. We&#8217;ll come back to the Application Context config file in a bit&#8230;</p>
<p><strong>Create an Employee Delegate Interface</strong></p>
<p>If you look at the AS and XML BDs, you&#8217;ll notice that they both have the same method signature for getList():</p>
<pre><code>public function getList():void</code></pre>
<p>We&#8217;ll use this as the point of commonality to create our interface, as they both need to actually get the list of employees for our application.</p>
<p><strong>IEmployeeDelegate.as</strong></p>
<pre><code>/**
 * Web App Solution Confidential Information
 * Copyright 2009, Web App Solution, Inc.
 *
 * @author Brian Riley
 * @date May, 4, 2009
 */
package com.wasi.employeeconsole.business.delegates
{
	public interface IEmployeeDelegate
	{
		function getList():void
	}
}
</code></pre>
<p>Next we&#8217;ll make each of our BDs implement the interface.</p>
<pre><code>public class EmployeeASDelegate implements IEmployeeDelegate
public class EmployeeXMLDelegate implements IEmployeeDelegate</code></pre>
<p><strong>Spring ActionScript Application Context</strong></p>
<p>The SAS Application Context (&#8221;SASAC&#8221;) is the XML configuration file that&#8217;s loaded at run-time by the Spring framework that defines all the concrete implementation objects you wish to use in your application &#8212; it essentially maps interface properties in classes (like our delegate:IEmployeeDelegate property in our GetEmployeeListCommand) to fully qualified, concrete objects. Let&#8217;s actually create our ApplicationContext and see how we can either inject our AS mock-object BD or our XML BD at run-time.</p>
<p>Create a new XML file called &#8220;ApplicationContext.xml&#8221; and save it in our project in a root folder called /assets/springactionscript/. Add the following code, which we&#8217;ll discuss in a moment:</p>
<p><strong>ApplicationContext.xml</strong></p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;objects xmlns              = "http://www.pranaframework.org/objects"
         xmlns:xsi          = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://www.pranaframework.org/objects http://www.pranaframework.org/schema/objects/prana-objects-0.5.xsd"&gt;

	&lt;!-- =================================================================== --&gt;
	&lt;!-- This application context defines multiple test configurations, it
		 can be expanded to include a deployment configuration as well,
		 please see comments below.

		 1. Testing: ActionScript Mock Delegates
		 2. Testing: XML Mock Delegates (Will add in next revision)
		 3. Deployment: Remote Object Delegates (Will add in next tutorial)

		 To use a configuration, uncomment it and comment the one you don't
		 want to use. By default, the Mock Delegates configuration is used.
	--&gt;
	&lt;!-- =================================================================== --&gt;

	&lt;!-- =================================================================== --&gt;
	&lt;!-- 1. Testing: ActionScript Mock Delegates &amp; Services --&gt;
	&lt;!-- =================================================================== --&gt;

	&lt;object
		id="delegateLocator"
		class="com.wasi.employeeconsole.business.delegates.DelegateLocator"
		factory-method="getInstance"&gt;

		&lt;property name="employeeDelegate"&gt;
			&lt;object class="com.wasi.employeeconsole.business.delegates.EmployeeASDelegate" /&gt;
		&lt;/property&gt;

	&lt;/object&gt;

&lt;/objects&gt;
</code></pre>
<p>Walking through the SASAC XML document from the top, you&#8217;ll notice the first-child node called <strong>&lt;object&gt;</strong>; each object tag defines a concrete object that we&#8217;ll inject into the application at run-time, as well as any properties (ie more concrete implementaitons) that we might inject into the object itself. The first object tag defines a new object that we haven&#8217;t discussed yet called the Delegate Locator (&#8221;DL&#8221;) (see <a href="http://www.allenmanning.com/?p=25" target="_blank">Allen Manning&#8217;s Post on SAS + CGM</a> for additional info on the DL in SAS), which is similar to the standard Service Locator (&#8221;SL&#8221;) object in CGM. However, instead of containing a registry of services like the SL, it contains a registry of BDs.</p>
<p>Taking a deeper look at DL object, you can see the fully qualified path to the DL object in it&#8217;s <strong>class</strong> attribute (we&#8217;ll create the AS class for the DL in the next section), and an attribute called <strong>factory-method=getInstance</strong>, indicating that it&#8217;s a Singleton. Next we&#8217;ll examine the <strong>&lt;property&gt;</strong> node, which is really the crux of the entire framework &#8212; here we&#8217;re setting the class property on the DL called <strong>employeeDelegate</strong> to a value of the fully qualified name of out AS mock-object delegate called EmployeeASDelegate, signifying that we&#8217;d like to inject the AS BD into the DL at run-time. Later we will setup the SASAC to inject the XML BD, but for now let&#8217;s test out this version by loading the file at run-time and making sure that it does in fact inject our AS BD.</p>
<p><strong>Create the DelegateLocator</strong></p>
<p>Since our DL is really just a registry of the BDs we want to use at run-time, we need to create a getter and setter method for each BD we want to inject into it with the same property name as the one we used in the SASAC &#8212; <strong>employeeDelegate. </strong>Create a new AS class called DelegateLocator and make it a singleton. Next, create a private var representing the employeDelegate with corresponding getters and setters and make sure they use our Employee Delegate interface, IEmployeeDelegate as the type:</p>
<pre><code>/**
 * Reference to the employee delegate. Provides an interface
 * to get the injected delegate at run-time.
 */
private var _employeeDelegate:IEmployeeDelegate;
public function get employeeDelegate():IEmployeeDelegate
{
	return this._employeeDelegate;
}
public function set employeeDelegate(delegate:IEmployeeDelegate):void
{
	this._employeeDelegate = delegate;
}</code></pre>
<p><strong>Modify the GetEmployeeCommand to Use the DelegateLocator</strong></p>
<p>If you recall, we recently modified the GetEmployeeCommand to reference the EmployeeXMLDelegate&#8230;we&#8217;ll need to go back into the CMD and modify it again to use the DL and Interface to acquire the injected BD:</p>
<pre><code>private var delegate:IEmployeeDelegate;

public function GetEmployeeListCommand()
{
	trace("GetEmployeeListCommand Constructor");
	this.delegate = DelegateLocator.getInstance().employeeDelegate;
	this.delegate.responder = this;
}</code></pre>
<p>Now we&#8217;re acquiring the BD from the DL with the injected, concrete implementation set up in the ApplicationContext file, but we need to make a couple small change to our BDs and Interfaces&#8230;we need a reference to the CMD as the responder. In our previous implmentation of the CMD, we passed in a reference of itself into the constructor of the BD, but now we&#8217;re going to set it as a property on the BD. Instead of making this change in ever BD, let&#8217;s make an abstract BD that each of our concrete BDs will subclass:</p>
<p><strong>AbstractDelegate</strong></p>
<pre><code>package com.wasi.employeeconsole.business.delegates
{
	import mx.rpc.IResponder;

	public class AbstractDelegate
	{
		/**
		 * Constructor.
		 */
		public function AbstractDelegate()
		{
			trace("AbstractDelegate Constructor");
		}

		/**
		 * RPC Responder, used as a reference back to the command that made the request.
		 */
		private var _responder:IResponder;
		public function get responder():IResponder
		{
			return this._responder;
		}
		public function set responder(value:IResponder):void
		{
			this._responder = value;
		}

	}
}</code></pre>
<p>Now go into each BD and subclass our new AbstractDelegate:</p>
<pre><code>public class EmployeeASDelegate extends AbstractDelegate implements IEmployeeDelegate
public class EmployeeXMLDelegate extends AbstractDelegate implements IEmployeeDelegate</code></pre>
<p>And finally go into our IEmployeeDelegate and extend a SAS CGM extention called <strong>IResponderAware</strong> that will allow/force each of our BDs to have a set repsonder(value:IResponder) method (open up the source of IResponderAware to see what it&#8217;s doing):</p>
<pre>public interface IEmployeeDelegate extends IResponderAware</pre>
<p>I know, I know&#8230;this is getting long, so on with it already&#8230;</p>
<p><strong>Load the Spring ApplicationContext Config</strong></p>
<p>Finally, open your Application file so we can load the SASAC and instantiate the DL:</p>
<ol>
<li>Create a class property representing the application context.</li>
<li>Listen for the Creation Complete event of the Application and handle it with an init() method.</li>
<li>Create loadSpringAppContext() method and call it in the init().</li>
<li>In the loadSpringAppContext() method, create a new FlexXMLApplicationContext (the application context class property we defined in step 1) and pass in the URL to the SASAC file.</li>
<li>Create listeners for the success and failed requests for the SASAC.</li>
<li>Load the File.</li>
<li>Upon successful retrieval of the SASAC, instantiate the DL.</li>
<li>Test&#8230;you&#8217;ll get an error, but this is exected and requires some explanation.</li>
</ol>
<pre><code>/**
 * Defines the application specific context by which objects and
 * their dependencies are loaded.
 */
private var applicationContext:FlexXMLApplicationContext;

</code><code>/**
 * Called when the Application component has been created.
 */</code><code>
private function init():void
{
	trace("Application init");
	this.loadSpringAppContext();
}

/**
 * Initializes the <code>applicationContext</code> instance and adds
 * listeners for the context file loading events.
 */
private function loadSpringAppContext():void
{
	trace("Application loadSpringAppContext");

	var applicationContextURL:String;

	applicationContextURL = "../assets/springactionscript/applicationContext.xml";

	// Initializes the <code>applicationContext</code> instance and adds listeners for the context file loading events.
	this.applicationContext = new FlexXMLApplicationContext(applicationContextURL);
	this.applicationContext.addEventListener(Event.COMPLETE, applicationContextLoadResult);
	this.applicationContext.addEventListener(IOErrorEvent.IO_ERROR, applicationContextLoadFault);
	this.applicationContext.load();
}

/**
 * <code>IApplicationContextLoader.applicationContextLoadResult</code>
 * implementation which handles successful loading of the context
 * document.
 */
public function applicationContextLoadResult(event:Event):void
{
	trace("Application applicationContextLoadResult");
	this.applicationContext.getObject("delegateLocator");
}

/**
 * <code>IApplicationContextLoader.applicationContextLoadFault</code>
 * implementation which handles an exception when loading the context
 * document.
 */
public function applicationContextLoadFault(iOErrorEvent:IOErrorEvent):void
{
	trace("Application applicationContextLoadFault " + iOErrorEvent.text);

	// we'll go with a simple stoopid apprach for this right now and just throw an alert
	Alert.show("Spring Application Context Failed to Load", "Error");
}
</code></pre>
<p>An error!?! WTF mate?</p>
<p><strong><a name="class-ref-error">Why Did We Get An Error?</a></strong></p>
<pre><code>[SWF] Users:brianmriley:projects:spring-hibernate:workspaces:flex3:EmployeeManagementConsole2:bin-debug:EmployeeManagementConsole2.swf - 1,235,684 bytes after decompression
CairngormFrontController Constructor
CairngormFrontController addCommands
Application init
Application loadSpringAppContext
5/8/2009 14:40:02.804 [INFO] SpringActionScript.FlexXMLApplicationContext Loading XML object definitions from [../assets/springactionscript/applicationContext.xml]
Error: A class with the name
        'com.wasi.employeeconsole.business.delegates.EmployeeASDelegate'
        could not be found.
	at as3reflect::ClassUtils$/forName()[C:\Users\Christophe\workspace\as3reflect\src\as3reflect\ClassUtils.as:89]
	at org.springextensions.actionscript.ioc.factory.support::DefaultListableObjectFactory/getObjectNamesForType()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\ioc\factory\support\DefaultListableObjectFactory.as:87]
	at org.springextensions.actionscript.context.support::XMLApplicationContext/registerObjectPostProcessors()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\context\support\XMLApplicationContext.as:196]
	at org.springextensions.actionscript.context.support::XMLApplicationContext/afterParse()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\context\support\XMLApplicationContext.as:158]
	at org.springextensions.actionscript.ioc.factory.xml::XMLObjectFactory/_doParse()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\ioc\factory\xml\XMLObjectFactory.as:341]
	at org.springextensions.actionscript.ioc.factory.xml::XMLObjectFactory/_loadNextProperties()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\ioc\factory\xml\XMLObjectFactory.as:315]
	at org.springextensions.actionscript.ioc.factory.xml::XMLObjectFactory/_loadNextConfigLocation()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\ioc\factory\xml\XMLObjectFactory.as:291]
	at org.springextensions.actionscript.ioc.factory.xml::XMLObjectFactory/_onLoaderComplete()[C:\Users\Christophe\Documents\Adobe Gumbo MAX Preview\spring-actionscript\core\src\main\actionscript\org\springextensions\actionscript\ioc\factory\xml\XMLObjectFactory.as:241]
	at flash.events::EventDispatcher/dispatchEventFunction()
	at flash.events::EventDispatcher/dispatchEvent()
	at flash.net::URLLoader/onComplete()
</code></pre>
<p>Since we&#8217;re injecting our concrete types of BDs into the application at run-time, we never explicitly told the Flex compiler to add our EmployeeASDelegate class into the SWF; thus, when we try to instantiate it the Flash Player throws an error saying the class doesn&#8217;t exist&#8230;one way to get around this is to just create a reference to it somewhere in your application, but we&#8217;re going to take a slightly different approach and add it to a property file for better organization.</p>
<p><strong>ClassReferences.properties</strong></p>
<p>Create a new properties file and put it directly into the src folder of your project (again, we&#8217;ll come back to cleaning this up in later posts) and create a reference to both the EmployeeASDelegate and the EmployeeXMLDelegate, since we&#8217;ll be switching back and forth between the 2 shortly.</p>
<pre><code>EmployeeASDelegate  = ClassReference("com.wasi.employeeconsole.business.delegates.EmployeeASDelegate")
EmployeeXMLDelegate = ClassReference("com.wasi.employeeconsole.business.delegates.EmployeeXMLDelegate")</code></pre>
<p>Now create a reference to your ClassProperties.properties file in your main Application:</p>
<pre><code>[ResourceBundle("ClassReferences")]
private var springClassRefs:ResourceBundle;</code></pre>
<p><strong>Success &amp; Last Touches</strong></p>
<p>Run your application and click the &#8220;Refresh List&#8221; button and you should see the following:</p>
<div class="wp-caption alignnone" style="width: 523px"><img title="Employee Mgmt Console Populated with AS Mock Objects" src="http://www.webappsolution.com/images/blog/emp-mgmt-console-as-mock-objs-view.png" alt="Employee Mgmt Console Populated with AS Mock Objects" width="513" height="350" /><p class="wp-caption-text">Employee Mgmt Console Populated with AS Mock Objects</p></div>
<p>Open up the SASAC file and change the reference of the Employee Delegate from the AS impl to the XML impl:</p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;objects xmlns              = "http://www.pranaframework.org/objects"
         xmlns:xsi          = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://www.pranaframework.org/objects http://www.pranaframework.org/schema/objects/prana-objects-0.5.xsd"&gt;

	&lt;object
		id="delegateLocator"
		class="com.wasi.employeeconsole.business.delegates.DelegateLocator"
		factory-method="getInstance"&gt;

		&lt;property name="employeeDelegate"&gt;
			&lt;object class="com.wasi.employeeconsole.business.delegates.EmployeeXMLDelegate" /&gt;
		&lt;/property&gt;

	&lt;/object&gt;

&lt;/objects&gt;</code></pre>
<p>Save it, run it, and you should see the following:</p>
<div class="wp-caption alignnone" style="width: 552px"><img title="Employee Mgmt Console with XML Business Delegate" src="http://www.webappsolution.com/images/blog/emp-mgmt-console-xml-data-view.png" alt="Employee Mgmt Console with XML Business Delegate" width="542" height="368" /><p class="wp-caption-text">Employee Management Console with XML Business Delegate</p></div>
<p>Voila! You&#8217;ve just successfully set up your first Flex + SAS + CGM application and learned how to inject 2 different types of BDs at runtime. Say tuned for episode 3 to learn how to inject the XML HTTPService in the EmployeeXMLDelegate also via SAS.</p>
<p><strong>References</strong>:</p>
<ul>
<li><a title="Allen Manning's Spring + Cairngorm Tutorial" href="http://www.allenmanning.com/?p=25" target="_blank">Dependency Injection in Flex Applications - Part 1 - Spring ActionScript and Cairngorm</a> - Allen Manning</li>
<li><a title="IoC and the Dependency Injection Pattern in Flex" href="http://www.ericfeminella.com/blog/2008/09/21/dependency-injection-iocdi-in-flex/" target="_blank">IoC and the Dependency Injection Pattern in Flex</a> - Eric Feminella</li>
<li><a href="http://www.herrodius.com/blog/" target="_blank">Christophe Herreman&#8217;s Blog</a> - Christophe Herreman</li>
</ul>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+%2B+Tomcat+%2B+WebORB%2FBlazeDS+%2B+Spring+Java+%2B+Hibernate+%2B+MySQL+Tutorial+Part+2+http://kdwdz.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.webappsolution.com/wordpress/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Flex+%2B+Cairngorm+%2B+Spring+ActionScript+%2B+Tomcat+%2B+WebORB%2FBlazeDS+%2B+Spring+Java+%2B+Hibernate+%2B+MySQL+Tutorial+Part+2+http://kdwdz.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webappsolution.com/wordpress/2009/05/08/flex-spring-actionscript-cairngorm-tomact-blazeds-spring-hibernate-mysql-pt2/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

