Picture 4.pngflexlogo.pngIn my previous post I have created a flex client with a BlazeDS back-end using the spring blazeds integration. If you are familiar with the springframework, the configuration became a lot easier at the server side. Still I had some problems to get it easier at the client as well. I still needed the services-config.xml file with some destinations in it. In the side note I already mentioned another way of creating the client using Spring-ActionScript project. As it turns out, you do not really need Spring-ActionScript to loose the configuration. Still it gives you some help and I like to experiment with it a litle bit.

This post is going through the steps to refactor the current books-overview sample application of mine into a cleaner project using Spring-ActionScript.

Obtaining and building Spring-ActionScript

The Spring ActionScript project is based on the prana framework. Christophe Herreman has moved it under the umbrella of SpringSource. Prana is an inversion of control container for flex that used the springframework approach. Now that prana is changed into Spring ActionScript, no releases have been made yet. So you need to create one yourself or download snapshot builds. Creating it yourself is very easy. Ant is used to create a release.

First download the source using a subversion client from the following url:


Step into the ant folder and execute “ant” to see the options. You can create a debug enabled version, a release, documentation. I created the debug enabled version with the following command. Of course you do have to have ant on your path. Do not forget to change the build.properties. You need to change the FLEX_HOME parameter as described in the readme file that comes with the sources.

ant compile-main-debug

The swc file is created in the antbuild/compile/main/swc folder. You can install it in your maven repository using the command.

mvn install:install-file -DgroupId=org.springframework -DartifactId=spring-actionscript -Dversion=0.6.1-SNAPSHOT -Dpackaging=swc -Dfile=antbuild/compile/main/swc/spring-actionscript.swc

What about maven

Maven is not really supported (yet). It is not very hard to use. If you followed the previous step, you now have the spring-actionscript artifact with version 0.6.1-SNAPSHOT in your repository. Defining your dependency and using the flex-compiler-mojo makes it very easy to create the swf file for your project. The following code block gives the important part of the pom file.



Recap of RemoteObject wrapper

For those of you that have not red any of my previous posts, you can find the sources of the sample I use at my google code project. If you want to read more about the sample and it’s structure I recommend my previous post: Integrating spring security and flex 3. In that post I describe the structure of my RemoteService that wrappes a RemoteObject and creates a listener for FAULT events that are thrown by RemoteObjects. This way you can create an exception handling mechanism for remote objects.

I did have to make a small change for the new endpoint configuration. I changed the constructor, added an extra argument for destination. The destination is attached to the wrapped RemoteObject.

    public function RemoteService(id:String, destination:String, endPoint:String) {
        this.remoteObject = new RemoteObject(id);
        this.remoteObject.destination = destination;
        this.remoteObject.endpoint = endPoint;
        this.remoteObject.addEventListener(FaultEvent.FAULT, onRemoteException);

Configuring an end point

Endpoint need to be configured on the server as well as on the client. In my previous post I already discussed the server side. With minimal configuration a spring bean is now exposed as a remote destination. Still the client needs to know these destinations. Previously I used the the same config file as from the server, but with the new spring blazeDS integration this was not working. At first I created two files, one for the server and one for the client. This is not ideal, therefore I started looking at configuring the endpoint in code. It turns out this is pretty straightforward. The code block before already shows how to add the destination to a RemoteObject. What does a destination look like?


This is not very flexible, and you do not want to hard code this in you swf file. Now is a good time to introduce the Spring ActionScript project. It is time to configure the services and inject the configuration into these components using data from a property file.

Using Spring ActionScript

In this sample we are going to use Spring ActionScript (from now on I call it S-AS) to configure the services. I do not introduce interfaces just yet. Of course it would be better to create interfaces for the different services that we use, that will be a nice improvement for the future. The following code block shows you the xml configuration file of the S-AS. This file is placed on the web server where the swf file is hosted as well. To be honest, I am not sure if this is what you really want. I need to think about that more. Next to the xml file is also a property file that specifies the parameters for host, port and web-context.

<objects xmlns="http://www.pranaframework.org/objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <property file="application-context.properties"/>

    <object id="bookService" class="services.BookService">
        <constructor-arg ref="0" value="bookService"/>
        <constructor-arg ref="1" value="remoteBookManager"/>
        <constructor-arg ref="2" value="http://${host}:${port}/${context-root}/messagebroker/amf"/>

    <object id="authenticationService" class="services.SecurityService">
        <constructor-arg ref="0" value="authenticationService"/>
        <constructor-arg ref="1" value="authenticationHelper"/>
        <constructor-arg ref="2" value="http://${host}:${port}/${context-root}/messagebroker/amf"/>


The next step of course is using S-AS in your project. How do you load the file and get the objects from the context. There are some steps you need to make, mainly because of the a-sychronous nature. In my application I show an initialization screen while the context is being loaded. When loading is done (you receive an event) I check if there is a current login for the user. The following code block shows part of the Main.mxml file. The initializeApplication function is called by the engine on creationComplete.

private var _applicationContext:XMLApplicationContext;

private function initializeApplication():void {
    _applicationContext = new XMLApplicationContext("applicationContext.xml");
    _applicationContext.addEventListener(Event.COMPLETE, onApplicationContextComplete);


private function onApplicationContextComplete(event:Event):void {
    bookService = _applicationContext.getObject("bookService");
    authenticationHelper = _applicationContext.getObject("authenticationService");

Line 4 shows the loading of the context, a call to the server is done to obtain the file. In line 5 we add the listener for the COMPLETE event. When this event is received we obtain the two services: bookService and authenticationService.


I have shown how you can use Spring ActionScript to loose the services-config.xml. You now have a clean separation between the server component and the client. There is a good start to structure the flex app, but it is only a start. I recommend to use another framework like Cairngorm puremvc or Mate. There is no integration yet between spring actionscript and Mate but I do think it will be a nice combination. If I can find the time I’ll try to come up with this.

Hope you liked this article, feel free to comment with remarks or questions.


Creating the flex client using Spring ActionScript
Tagged on: