↓ Archives ↓

BlazeDS with Spring

With the release of BlazeDS from Adobe building your business logic in java just became a lot more accessible! In this post I’m going show the basics of setting up a Java, spring based application, then connecting to it from Flex via the flex.messaging.factory.SpringFactory . I’m going to keep the the domain (business logic) as simple as possible, and I’m not going to be persisting anything to a database. This is so I don’t get bogged down in details and can get straight to the point… connecting Flex to Java/Spring with BlazeDS…. so here we go!

What you’ll need.

The Steps!

Creating The Java

  1. Create a Spring project in eclipse named BlogExample and set the output folder to be war/WEB-INF/classes.
  2. Find the directory you unzipped the BlazeDS download into and navigate to the following directory {blaze install dir}/tomcat/webapps/blazeds/
  3. copy the contents of this directory into the new project (you should have copied two folders, called WEB-INF and META-INF. These folders contain the necessary libraries and config files for connecting to Java from Flex)
  4. Copy spring.jar file from {Spring unzip directory}/dist/ into the war/WEB-INF/lib directory of the project. The project now contains all the necessary library’s to connect Flex to Java. However as we are using Spring we will still need one more jar file which we will get in step 9
  5. Next you need to create your business logic, I have created a very simple POJO called SimpleBook.java, which contains two properties with getters and setters (you can download the complete project here which contains this simple class).
  6. Create a new Spring Bean Definition File called beans.xml inside the WEB-INF directory.
  7. Add the following lines to the new beans.xml file, it tells spring to instantiate SimpleBook and set its properties
    <bean id=”myBook” class=”uk.co.ziazoo.example.domain.SimpleBook”>
    <property name=”name” value=”my book” />
    </bean>
  8. Next we need to edit the services-config.xml and remoting-config.xml so that flex can connect to the application. Your can download mine from here (remoting-config.xml, services-config.xml). Be sure to change the endpoint in the services-config.xml to point to your local tomcat server. I have used the tomcat that come with the BlazeDS download as it comes pre-configured to work with BlazeDS.
  9. If you take a look within the services-config.xml you will see I am referencing a class named flex.messaging.factory.SpringFactory which doesn’t currently exist in the project. We can get this file thanks to http://www.igenko.org… here blazeds-spring-beta1.jar
  10. Once you have downloaded the blaze-spring-beta1.jar just copy it into the war/WEB-INF/lib folder
  11. Next its time to configure the web.xml file. The web.xml file that come with BlazeDS needs a little tweaking to get it to work with our spring app. Firstly the included file uses the older DTD based syntax, we need to change this to the newer XML scheme method. Once we have done that we need to change the <listener> property such that it fits the spring based development ideas. (The changes essentially allow Spring to instantiate the business classes, rather than letting Flex do it… this is crucial to the whole idea of Spring, and its called IoC, Inversion of Control). Once that changes are made the web.xml file should look like this web.xml
  12. The Java code is now complete, all that remains is to deploy it to the tomcat server, I have done this using Ant, blogging how to setup Ant and starting tomcat are all fairly in depth and very dependent on the system they are being installed on, so I’ll keep quite on that. If you are stuck I recommend reading the build.properties and build.xml files in my project.

The Flex

I am using Flex Builder 3.0, so these steps will vary for other IDS etc

  1. Create a new project and select the application type J2EE
  2. Set the root folder to to the context root of the java app withing tomcat ie /Users/Sam/Documents/blazeds_b1_121307/tomcat/webapps/blogexample
  3. Replace the contents of the main mxml file with the follow main.mxml
  4. Run the flex app.. and you should see the following
    Result

And thats your lot!!! :)

OOPS… forgot to upload the full Java code… here ya go Java code

11 Comments

  • Jan 15th 200817:01
    by hatsix

    Reply

    Hi Sam,
    Thanks for the bit of code, I’m attempting to port over a simple WebORB app that I just wrote (WebORB doesn’t support RTMP), but I’m having some issues with deploying/installing my app. Do you know of any resources that could help me with this? (specifically, the usage/differences between deploy, deploywar, install)

    Also, I’m planning on blogging about my experience, and I’m not embarrassed to say that I’ve lifted more than a bit of your code. However, since you haven’t licensed it, I’m not technically able to re-post it. Would you be willing to release it under the MIT License ?

  • [...] I decided I wanted some of the Messaging/Polling features offered in BlazeDS, and after reading this post, I decided to try porting over to BlazeDS. WebORB does a lot of hand-holding, and BlazeDS [...]

  • Jan 23rd 200819:01
    by leedm777

    Reply

    The main.mxml link isn’t working. You get:

    The page you are requesting cannot be served due to the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognised, and is therefore not allowed.

  • Jan 23rd 200819:01
    by Sam

    Reply

    Sorry about that, for some reason it didn’t want to serve mxml files, so I have added a .xml to the end of it.. should work now :)

  • Jan 31st 200812:01
    by pabloapa

    Reply

    Hi,

    I found this post very interesting, as I’m moving a Flex app to Remoting + Spring. While testing a bean of my own, I keep getting the error “[MessagingError message='Destination 'SomeBean' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']“. The bean I’m invoking is not directly on the deployed WAR, but in the containing EAR instead, and deployed by Spring on its start.

    Second issue (derived): I read that MXML files using BlazeDS should be compiled using the -services argument, pointing to the services-config.xml file on the server. Is this right?

    Thanks and great work!

  • Jan 31st 200813:01
    by Sam

    Reply

    Hey pabloapa,

    I would guess the best thing to do when you see that error is to verify that your Flex and Java back end can communicate. I would do this by setting up a really simple bean in your beans descriptor file, for example something that just echos a string back to flex. For that to work, as you pointed out you will need to add a services argument to the mxmlc call, something of the form below should do the trick!
    -services “[local path to your java project]/WEB-INF/flex/services-config.xml”

    once you are confident that the connection to the simple bean is working, then move to setting up the required connections for your application.

    Hope this helps!!

    Sam

  • [...] There is a blog from Sam Williams describing his experiences with BlazeDS and Spring [...]

  • Pingback

    Oct 14th 200805:10
    by It’s all about RIA

    Reply

    [...] Sam Williams ::

  • Pingback

    Oct 14th 200806:10
    by Flex with Java « It’s all about RIA

    Reply

    [...] Sam Williams ::

  • Pingback

    Oct 14th 200806:10
    by Blazeds « It’s all about RIA

    Reply

    [...] Sam Williams ::

  • Pingback

    Oct 14th 200809:10
    by BlazeDS « Flex Generation Weblog

    Reply

    [...] Sam Williams ::

  • Leave a Reply