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:

https://src.springframework.org/svn/se-springactionscript-as/

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.

    <dependencies>
        <dependency>
			<groupId>com.adobe.flex.framework</groupId>
			<artifactId>flex-framework</artifactId>
			<version>${flex.sdk.version}</version>
			<type>pom</type>
		</dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-actionscript</artifactId>
            <version>0.6.1-SNAPSHOT</version>
            <type>swc</type>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/flex</sourceDirectory>
        <plugins>
            <plugin>
				<groupId>info.flex-mojos</groupId>
				<artifactId>flex-compiler-mojo</artifactId>
                <version>${flex-mojos.version}</version>
				<extensions>true</extensions>
				<configuration>
                    <contextRoot>/books-web</contextRoot>
					<locales>
						<locale>en_US</locale>
					</locales>
				</configuration>
			</plugin>
        </plugins>
        <defaultGoal>install</defaultGoal>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
    </build>

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?

http://localhost:8080/books-web/messagebroker/amf

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"
         xsi:schemaLocation="http://www.pranaframework.org/objects

http://www.pranaframework.org/schema/objects/prana-objects-0.7.xsd">

    <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>

    <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"/>
    </object>

</objects>

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);
    _applicationContext.load();

    showRightUIComponent('initializingMessage');
}

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

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.

Conclusions

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.

References

Tagged on:         

9 thoughts on “Creating the flex client using Spring ActionScript

  • Pingback: Spring ActionScript – Dependency Injection and more for Flex/AIR | rainwebs.net

  • July 7, 2009 at 10:32 pm
    Permalink

    Hi, I know this post was a while ago.

    Have you solved the problem of integrating spring-action script with Mate? I’m using Mate at the moment but there are a few things I don’t like about it that I think the spring actionscript would be better at.

    I guess somehow the ObjectBuilder/generator tags from mate need to hook into Objects created in the spring action script, haven’t looked to hard and wondering if you had come up with anything?

    Steve

    Reply
    • July 7, 2009 at 10:39 pm
      Permalink

      To be honest, the only thing I have used spring-actionscript for is to load a file from the server that helps me configure the remote connection. Other than that I was able to stick to Mate. So I am afraid I did not do what you are looking for.

      Reply
  • Pingback: Spring ActionScript - Dependency Injection and more for Flex/AIR at Springsteam - Next Generation Java Development | Spring . Flex/AIR . 3D

  • January 13, 2009 at 6:45 pm
    Permalink

    OK So I managed to run your sample.
    Here is what I had to do.
    It looks like I faced a couple of issues.
    on my first try, I compiled the springactionscript swc using the “open source” flex sdk,
    I later tried with the adobe one.

    Then I faced a “java.util.zip.ZipException: The system cannot find the path specified”, it looked the same as this hibernate entity manager bug : http://opensource.atlassian.com/projects/hibernate/browse/EJB-121

    So I switched to a more recent version of hibernate entity manager :3.3.2.GA.
    But then it looked like the pom file of this version was missing xalan, so I had to add it manually.

    That’s what I ended up editing in the books-integration pom file:

    org.hibernate
    hibernate-entitymanager
    3.3.2.GA

    xalan
    xalan
    2.7.1

    And then finally to get the swf properly deployed on jetty
    I ran mvn jetty:run-exploded.
    And that’s it !

    Finally I wanted to tell you that I went a lot further than the xdoclet stuff you saw on my adobe blog, I’m actually blogging a lot more on my personal blog : http://www.droff.com

    and you’ll see a lot more visiting those links :
    * http://fna.googlecode.com/svn/trunk/fna/site/flex-contrib-spring/index.html
    * http://fna.googlecode.com/svn/trunk/fna/site/mvn_archetypes/blazeds-autowired-spring-hibernate-archetype/index.html

    Cheers and thanks for sharing this great integration work of yours !
    François

    Reply
    • January 14, 2009 at 8:15 am
      Permalink

      Thanks for this investigation, I’ll try it out on the mac and see what happens.

      I’ve put your blog to my rss feeds, keep the posts coming. I prefer english though :-), my french is a bit rusty.

      Reply
      • February 9, 2012 at 9:05 am
        Permalink

        Questions from today, if smeoone’s willing to dig into them:1. What’s happening if we omit any class access modifiers from the class definition?2. Is there some way to set the warning level of compilation, so warnings will prevent the application from execution, just as ordinary errors do?

        Reply
  • January 6, 2009 at 5:14 pm
    Permalink

    Thanks.
    I read your post with interest. I tried your sample on google code.
    I ended up with a webapp initialization error:

    17:07:02,124 ERROR DispatcherServlet:290 – Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean wit
    h name ‘mySpringManagedMessageBroker’ defined in ServletContext resource [/WEB-I
    NF/spring-web.xml]: Invocation of init method failed; nested exception is java.l
    ang.NoClassDefFoundError: org/apache/xpath/CachedXPathAPI

    Do we need to add xalan in the dependencies ?

    By the way, I’ve also been playing around with spring/blazeds/flex lately but before the springsource guys release this new library.
    The result was pretty elegant as well with autowired spring service (based on lib we named flex-contrib-spring), have a look at :
    * http://code.google.com/p/fna/
    * http://fna.googlecode.com/svn/trunk/fna/site/flex-contrib-spring/index.html
    * http://fna.googlecode.com/svn/trunk/fna/site/mvn_archetypes/blazeds-autowired-spring-hibernate-archetype/index.html

    Reply
    • January 6, 2009 at 6:10 pm
      Permalink

      Hi Francois, at the moment I cannot reproduce your error. I have downgraded to java 5, but than it works as well. What java version are you using? And what operating system?

      Of course I know the work you are doing at fna. I have been working with the archetypes as well. I haven’t used the one with blazeds that you mention. I am not that fond of XDoclet and code generation. But that is a personal choice. I like the work you are doing in getting maven better supported for flex.

      If you can try it out with xalan and it works for you, let me know, than it might be helpful for others as well.

      thanks

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>