Flex 4 & Spring 3 Integration


Integrating Flex And Spring

Adobe Flex has strong ties to Java, which include an Eclipse-based IDE and BlazeDS, its open source server-based Java remoting and web messaging technology.

By default BlazeDS creates instances of server-side Java objects and uses them to fulfill remote object requests. This approach doesn’t work with Spring, as the framework is built around injecting the service beans through the Spring container. The Spring integration with BlazeDS allows you to configure Spring beans as BlazeDS destinations for use as remote objects in Flex.

You will initially need:

1) An Eclipse 3.5 Distribution. Either You may choose either the standard Eclipse 3.5 (Galileo) for Java EE Developers (On Mac use the Eclipse Carbon version): http://www.eclipse.org/downloads/

For enhanced Spring support you can use the SpringSource Tool Suite:


2) Flash Builder 4 (60 day T rial) installed as a plug-in for the Java EE Eclipse


3) Tomcat 6: http://tomcat.apache.org/

4) BlazeDS 4 (Binary Distribution):


5) Spring Framework 3.0.2 (vanilla r elease):


6)  Spring BlazeDS Integration 1.0.3 (vanilla r elease):


7)  AOP Alliance 1.0:


8) backport-util-concurrent 3.1 for the Java version you’r e using:


9) cglib 2.2 http://sourceforge.net/projects/cglib/files/

10) asm 3.2 http://forge.ow2.org/projects/asm/

I think now we are ready with initial elements lets start with server setup

First install your chosen Eclipse  and then install Flash Builder 4 as a plug-in to the Eclipse distribution you just installed. Also extract the ZIP files for the other dependencies specified above. When you’ve completed these steps, run Eclipse. In Eclipse create a server that will run the application:

1. Choose File > New > Other

2. Select Server > Server

3. Click Next

4. Select Apache > Tomcat v6.0 Server

5. Click Next

6. Specify the location where Tomcat is installed and select the JRE(version 5 or higher) to use

7. Click Finish

Set up the server-side Java web project in Eclipse by creating a web application from the blazeds.war file

(found inside the BlazeDS zip file).

In Eclipse, import the blazeds.war file to create the project:

1. Choose File > Import

2. Select the WAR file option. Specify the location of the blazeds.war file.

For the name of the web project, type samplepring

3. Click Finish

First remove the xalan.jar file from the WebContent/WEB-INF/lib folder. Next, go into the project properties. One way to get there is by right-clicking on the project and selecting Properties. In the project properties, select Java Build Path and then click the Source tab. Set the Default Output Folder samplepring/WebContent/WEB-INF/classes

This causes all Java classes that are created in the project to be deployed in the web application.

In the WebContent/WEB-INF/flex folder update the services-config.xml file with the following contents:

<?xml version=”1.0” encoding=”UTF-8”?> 
<channel-definition id=”my-amf” class=”mx.messaging.channels.AMFChannel”> 
<endpoint url=”http://{server.name}:{server.port}/{context.root}/messagebroker/amf”
<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel"> 
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" 
<channel-definition id=”my-polling-amf” class=”mx.messaging.channels.AMFChannel”> 
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling"

Now select the Servers tab in Eclipse. If the tab is not visible you can make it visible by choosing Windows > Show View > Servers. Right-click the Tomcat Server and select Add and Remove. To add the samplepring web application to the server, select it in the Available list and then click Add. Finally, click Finish.Next, you need to add the required dependencies to the samplepring web application. Copy all of the Spring Framework libraries / JAR files (located in the dist folder) to the WebContent/WEB-INF/lib folder. Also copy the Spring BlazeDS Integration library (located in the dist folder) to the lib folder. Do the same for aopalliance.jar, backport-util-concurrent.jar, cglib-2.2.jar, asm-3.2.jar.

Simple Flex Remoting

To configure the server for simple Flex Remoting first edit the web.xml file in the WebContent/WEB-INF folder. Replace its contents with:
<web-app xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” version=”2.5”>

Eclipse may try to change the web-app version to 2.4 instead of 2.5. If this happens just manually change it back to 2.5.Spring will now handle requests to the /messagebroker/url.
Now configure Spring by creating an applicationContext.xml file in the WebContent/WEB-INF folder with the following contents:

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
<flex:remoting-service default-channels=”my-amf”/>
<context:component-scan base-package=”flex” />
Now create a simple Java class that will be exposed through the AMF channel to a Flex application. In the src folder create a new Java Class in the flex package with the name “HelloWorldService”. Set the contents of the HelloWorldService.java file to:

package flex;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
public class HelloWorldService {
public String sayHello(String name) {
return “howdy, “ + name;
The @Service annotation tells Spring that the class is a Service and the @RemotingDestination annotation exposes the class as a remoting endpoint for Flex. This class also contains a single method named sayHello, which simply takes a string and returns a slightly modified version of the string. By default all public methods on a class are available for remoting. You can keep a public method from being exposed as a remoting endpoint by using the @RemotingExclude annotation.

Create the Flex Application

Now you can create a Flex application that will call the sayHello method on HelloWorldService. To begin building the Flex application, simply create a new Flex Project in Eclipse. In the New Flex Project wizard, type sayHello as the name, select Web as the Application type, and set the Flex SDK Version to Flex 4.0 (usually the default). Also select J2EE as the Application Server Type, enable Use Remote Object Access Service, and select BlazeDS. Ensure that the Create Combined Java/Flex Project Using WTP option is not checked and then click Next. Now enter the information for your samplespring project. The Root folder is the WebContent folder in your samplespring project. The Root URL should be http://localhost:8080/samplespring/. The Context root should be /samplespring.
Click Finish to create the project. You should now see the template code for the application. Replace the code with the following:

<?xml version=”1.0” encoding=”utf-8”?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009”
<s:RemoteObject id=”ro” destination=”helloWorldService”
<s:TextInput id=”t”/>
<s:Button label=”sayHello” click=”ro.sayHello(t.text)”/>
<s:Label text=”{ro.sayHello.lastResult}”/>

When you save the file it should automatically compile. When it is compiled it should automatically be deployed in your web application.You are now ready to start the server and test the application. Go to

the Servers View in Eclipse and start the Tomcat server. Watch the console and make sure that there are no startup errors.Now run the sayHello application (one way is to right-click the sayHello.mxml file, select Run As, and then select Web Application). Your browser should open the sayHello.html file that then downloads and runs the Flex application. Type your name in the TextInput box and click the sayHello button. This will initiate a Flex Remoting request to the Tomcat server calling the Spring DispatcherServlet, which then will

look up the service based on the destination helloWorldService. This destination is automatically mapped to the HelloWorldService Spring Bean. Then the sayHello method will be invoked on the bean, passing

in the String that was specified in the RemoteObject call on the client. The method returns a new String, which is then serialized into AMF, inserted into the HTTP Response body, and sent back to the client. On

the client side the Flex application will parse the AMF and then set the ro.sayHello.lastResult property to what was returned from the server.Data binding in the Label will note the property change and refresh its view of the data.

Happy Coding…..

Total Page Visits: 2079 - Today Page Visits: 1

Leave a Reply

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