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.

Wow, springframework enters the ActionScript and flex domain
Tagged on: