Some time a go I had a chat with Alef Arendsen. We talked about flex and the integration with springframework. As a lot of you know, I wrote a few posts about that topic. We talked about what the springframework could do to make life easier for spring developers when using flex and BlazeDS. At that time I did not expect that springsource would jump into this the way they do now. I see already two projects

This sounds almost to good to be true. Combining my springframework knowledge with flex and ActionScript. Maybe I should create a flex front-end talking using BlazeDS to a Hippo Back-end. Of course the application is running on OSGi :-)

Oke, that is something I am not going to talk about in this post. I am going to talk about the new spring flex projects and my first impression. This post is about the BlazeDS integration.

I am going to use the books-overview sample to try out the integration. You can find the sources of books-overview at google code. I base the changes I do on the blog post from Jeremy Grelle about the release of M1. The books-overview project is build with maven. I was using a separate module for the flex remoting config files. Since these files now become very easy and do not need maintenance, I am placing them at the projects at hand. Therefore the config project is to be removed. And the files have been moved into the ria and the web project.

Maven

The hardest part with maven is to find the dependencies, I added two repositories and manually installed the blazeds spring integration.

    <repositories>
        <repository>
            <id>flex-mojos-repository</id>
            <url>http://svn.sonatype.org/flexmojos/repository/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>fnh</id>
            <url>http://fna.googlecode.com/svn/trunk/fna/fna_m2_repository/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>flex-mojos-repository</id>
            <url>http://svn.sonatype.org/flexmojos/repository/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

There are two important modules, the books-web and the books-ria. Books-web contains the BlazeDS stuff, books-ria the flex stuff. Check the poms if you need the dependencies. I do want to show the flex compilation using the plugin from velo.

    <dependencies>
        <dependency>
			<groupId>com.adobe.flex.framework</groupId>
			<artifactId>flex-framework</artifactId>
			<version>${flex.sdk.version}</version>
			<type>pom</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>

web.xml

We start of with the spring replacement of the blazeds servlet. Instead of the flex.messaging.MessageBrokerServlet we now use the standard dispatcher servlet of spring.

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-web.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

Spring config

I am not going to spend to much time on explaining this stuff since Jeremy already does a good job in his blog. The following code block gives the spring config I have created with some comments in there as well. In short, we create a wrapper around the Message broker, a url handler that connects the incoming requests to this message broker. Finally you need the services. This is one of the nice things of the spring integration. You can export your remote services from the spring config file. I expose the book manager as the remoteBookManager. Now check the source

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Bootstraps and exposes the BlazeDS MessageBroker -->
    <bean id="mySpringManagedMessageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean"/>

    <!-- Maps request paths at /* to the BlazeDS MessageBroker -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /*=mySpringManagedMessageBroker
            </value>
        </property>
    </bean>

    <!-- Dispatches requests mapped to a MessageBroker -->
    <bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter"/>

    <!-- remote proxies -->
    <bean id="remoteBookManager" class="org.springframework.flex.messaging.remoting.FlexRemotingServiceExporter">
        <property name="messageBroker" ref="mySpringManagedMessageBroker"/>
        <property name="service" ref="bookManager"/>
    </bean>
</beans>

That is it for the server side. We continue with the client

Client configuration

The client does not need to be changed a lot. We can now remove the special factory configuration for the connection of spring and flex. There is one thing that is still to be changed, the configuration of the destinations. For now I have an empty destination in my remoting-config.xml. I hope this is because I am doing something wrong. If that is the case I’ll correct this is the future. The next code block

<service id="remoting-service" class="flex.messaging.services.RemotingService">
    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>

    <destination id="remoteBookManager"/>

    <destination id="authenticationHelper">
        <properties>
            <source>nl.gridshore.samples.books.web.security.AuthenticationHelper</source>
        </properties>
    </destination>
</service>

Concluding

For now I do not see a lot of improvement in clarity, it does not get worse eaither. I can imagine if we make the configuration at the client side more easy it will get better. Of course the road map is interesting. Think about security. Now I have created some custom mechanisms for the security integration. It would be very nice if that is supported out of the box. So should you use it? Yes I think you should if you are into the spring framework. It looks like a clean way to integrate flex and spring and I do believe it will get better in the future.

In short, thanks SpringSource for this project, let’s hope this will give a push in the right direction.

Side note

By creating your own channels in code, you can accomplish losing the client configuration of destinations. I have made it work in an ugly way and will post more on this when I have made it nice. Go and habve a look at the post from kyle hayes if you want to learn more.

I personally like the approach of the other spring flex project. That is the direction I am going into: http://www.herrodius.com/blog/158.

Tagged on:         

19 thoughts on “Wow, springframework enters the ActionScript and flex domain

  • April 9, 2010 at 8:56 am
    Permalink

    Hello,
    Firstly thanks for the examples that you put on web… Even I couldn’t manage to make it work yet, I think It’s the most complex example that includes BlazeDS, Spring and Flex frameworks I see until now…
    Actually I am new to Maven for now that I think that’s why I am having these problems.
    My problem is that I really couldn’t manage to build a maven project that includes flex-mojos blazeds and spring all together. I have a project that use flex as front-side, spring as a glue, blazeDs as integration and hibernate for persistence. Actually all is working now in my eclipse workspace in seperate projects and depends each other and some libraries like apache-cxf for wsdltoJava operations and google collections and such. I think It’s enough for the introduction.
    Anyway my problem is that I want to convert my eclipse projects to maven projects that I can manage more easily and automatically myself and also for Hudson(my little CIT friend…) Anyway I successfully converted all my eclipse Java projects to maven and I can develop and use them from eclipse as well thankfully to m2eclipse plugin. But I still couldn’t manage to convert my flex project… I think I just need a working example that uses Flex, Spring and BlazeDs technologies together but I couldn’t find any working example yet. I checked out HEAD revision of the books-overview example from gridshore but it is also giving some errors… I checked repositories, dependencies and it seems there isn’t any problem in POM but still maven can’t find some artifacts and I really don’t know what to do… I just thinked that I wrote repositories wrongly at first because they are changing rapidly I think but there are right also…

    There are two artifacts missing for now…My error log is like this:

    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Failed to resolve artifact.

    Missing:
    ———-
    1) com.adobe.flex:compiler:pom:3.3.0.4852
    Path to dependency:
    1) org.sonatype.flexmojos:flexmojos-maven-plugin:maven-plugin:3.5.0
    2) com.adobe.flex:compiler:pom:3.3.0.4852

    2) com.adobe.flex.compiler:asdoc:zip:template:3.3.0.4852

    Try downloading the file manually from the project website.

    Then, install it using the command:
    mvn install:install-file -DgroupId=com.adobe.flex.compiler -DartifactId=asdoc -Dversion=3.3.0.4852 -Dclassifier=template -Dpackaging=zip -Dfile=/path/to/file

    Alternatively, if you host your own repository you can deploy the file there:
    mvn deploy:deploy-file -DgroupId=com.adobe.flex.compiler -DartifactId=asdoc -Dversion=3.3.0.4852 -Dclassifier=template -Dpackaging=zip -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

    Path to dependency:
    1) org.sonatype.flexmojos:flexmojos-maven-plugin:maven-plugin:3.5.0
    2) com.adobe.flex.compiler:asdoc:zip:template:3.3.0.4852

    ———-
    2 required artifacts are missing.

    for artifact:
    org.sonatype.flexmojos:flexmojos-maven-plugin:maven-plugin:3.5.0

    from the specified remote repositories:
    flexmojos-repository (http://repository.sonatype.org/content/groups/public/),
    central (http://repo1.maven.org/maven2),
    maven2-repository.dev.java.net (http://download.java.net/maven/2/),
    yoolab.org-releases (http://projects.yoolab.org/maven/content/repositories/releases)

    Thanks in advance for the help if I can get any… And thanks for your complicated example as well :)

    • April 9, 2010 at 9:08 am
      Permalink

      Hi, please check out the more recent posts of mine. Things are becoming much easier with the new spring blazeds integration project. The books overview sample is a good example of how easy things can become.

    • April 9, 2010 at 9:19 am
      Permalink

      Sorry, I did not read your post carefully enough. It is strange that this does not work for you. The adobe dependencies should come from the flexmojos repository. I’ll do some more investigation, but for me it seems to work.

      • April 9, 2010 at 9:40 am
        Permalink

        Actually I didn’t change anything from your trunk just checked out and run mvn package… But It didn’t work. What do you recommend me as an experienced maven user :) to delete all maven repository? to reinstall something or such? to research some new framework? I checked pom but it seems right to me as well. Just another question as well… you mention that there is a books-ria module on your blog about book project but there isn’t any project named like that in the trunk. So you moved your ria project to another project or did I miss anything there? Maybe if you can show me an updated example about Flex, Spring and BlazeDS, it will be most helpful for me… I just want something that outputs BUILD SUCCESSFUL to my screen :)
        Thanks for very quick answer… sorry I replied to the root thread… just my mistake :)

        • April 9, 2010 at 9:52 am
          Permalink

          Hmm, I removed my dependencies and indeed it seems to be broken. Need to check why it is broken. They probably changed something at flexmojos. Will reply again when I have it fixed.

          • April 9, 2010 at 10:15 am
            Permalink

            Really thanks for your efforts… FlexMojos project is still quick changing environment I think… or may be I couldn’t manage to use any stable version of flexmojos… :) I am waiting for your answer :)

          • April 9, 2010 at 10:24 am
            Permalink

            I think I fixed the problem, update the code and you see an updated pom. I added a pluginRepository that seems to be needed. Hope that works for you.

            As for the ria module, yes that has been replaced. I now use Mate as a MVC framework.

            I also have another example that you could have a look at using the parsley framework. But again this is not a very easy sample. You can find the code and more information about the sample here:
            http://code.google.com/p/axonframework/source/checkout

          • April 9, 2010 at 4:14 pm
            Permalink

            Tried to build different archtypes, tried to build examples of sonatype.org, tried to change my settings.xml, tried to build flexmojos sources as well… Nothing can’t be done today :( Only I could build this example(http://code.google.com/p/flex-blazeds-spring-archetype/) and run it, but I don’t know if it has spring or not, couldn’t check it… I am still trying, trying to be hopeful :)

          • April 12, 2010 at 12:56 pm
            Permalink

            Heya,
            I just checked out your repository, tried to build it with maven, mate dependency was missing, downloaded Mate_08_9.swc from http://mate.asfusion.com/page/downloads and manually installed it (mvn install:install-file -Dfile=Mate_08_9.swc -DgroupId=com.asfusion -DartifactId=mate -Dversion=0.8.9 -Dpackaging=swc)
            than ran this command:
            mvn package -DflashPlayer.command=C:\FlashPlayer\flashplayer_10_sa_debug.exe

            and…
            *********************************************************************************
            ————————————————————————
            Reactor Summary:
            ————————————————————————
            Books Overview Application ………………………. SUCCESS [1.750s]
            books-common …………………………………… SUCCESS [1.343s]
            books-domain …………………………………… SUCCESS [0.063s]
            books-integration ………………………………. SUCCESS [6.203s]
            books-business …………………………………. SUCCESS [0.953s]
            books-flex …………………………………….. SUCCESS [0.000s]
            books-flex-security …………………………….. SUCCESS [13.938s]
            books-flex-mate ………………………………… SUCCESS [17.515s]
            books-web ……………………………………… SUCCESS [6:47.610s]
            ————————————————————————
            ————————————————————————
            BUILD SUCCESSFUL
            ————————————————————————
            Total time: 8 minutes
            Finished at: Mon Apr 12 14:50:37 EEST 2010
            Final Memory: 40M/171M
            ————————————————————————

            ooooOOOhhhhooo i am so happy just looking how it is built now :)

      • February 9, 2012 at 9:01 am
        Permalink

        is still more dffsuie than I am used to. Most of the technical info I used was found in the Blaze DS Integration demo by Christophe

    • February 9, 2012 at 12:58 pm
      Permalink

      M.Zach,regarding your errros above – have you tried running the sample project listed in Page 2 > References?

  • April 20, 2009 at 5:36 am
    Permalink

    Figured it out. You are missing the Xalan dependency in the books-web POM. Add that, build from the top, and then mvn jetty:run-exploded launches your application successfully.

    • April 20, 2009 at 9:35 am
      Permalink

      Good to hear, I checked this morning. But I do not have the problem. I did add some repositories to get dependencies for spring flex libraries.

      regards Jettro

  • April 17, 2009 at 7:27 am
    Permalink

    When running mvn jetty:run-exploded from the books-web modules, I get the following error:

    2009-04-16 22:17:02.741:/books-web:INFO: Initializing Spring FrameworkServlet ‘Spring MVC Dispatcher Servlet’
    22:17:03,125 INFO FlexConfigurationManager:125 – Loading Flex services configuration from: ServletContext resource [/WEB-INF/flex/services-config.xml]
    22:17:03,134 ERROR DispatcherServlet:290 – Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘_messageBrokerDefaultHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘_messageBroker': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/xpath/CachedXPathAPI

    Any ideas?

    • April 17, 2009 at 7:33 pm
      Permalink

      Must be a maven thingy, not sure. What java version and os are you using? Check the package for the missing class

    • February 9, 2012 at 11:26 am
      Permalink

      Spring.  Christophe Coenraets, felolw evangelist, recently blogged about this in detail and did a segment on Adobe TV about

  • March 5, 2009 at 12:44 am
    Permalink

    Hi. I dont think you need to touch the remoting-config.xml for the spring-flex integration. The bean is exposed in the spring configuration file and it is available for remoting. Before the spring-flex integration, one needs to specify this end point.

  • January 5, 2009 at 7:57 pm
    Permalink

    Thanks for the post. I was going to post this myself and just saw yours in nick of time. Saved me some time

Comments are closed.