Alain Hufkens {Rich Interactive Applications Developer}

15May/080

Connecting an AIR client with BlazeDS using Spring/Hibernate

blaze_ds_logo_spots This article explains how to create an Adobe AIR application that uses Blaze Data Services to connect to a Java back-end. The Java back-en uses Spring/Hibernate to retrieve the data to the client. I will try to explain the steps from database up to the client.

You can download the source code for the sample here. I used IntelliJ IDEA and Flexbuilder 3 to build this sample. To run the web application you will need Apache Tomcat 6 with the BlazeDS jar files installed. You can find more information about how to do that on this page.

Create the database

You will need MySQL installed to run the demo. The included sample connects to a database installed on localhost. You can change the connection settings in the spring-dao.xml file. I will only use one table that contains information about people.

I know it's not that original :)

Database

Just create a new MySQL database and create the statement above. You can execute the create.sql file from the sample, that you can find in the sql folder.

Map the table to a Java Object using Hibernate

After creating the database we would need to write SQL code to fetch the people from the database. To avoid writing "boring" code, I will use Hibernate to facilitate this repetitive job. You will need to do extra configuration but this will pay of in the long run.

First we need to create the Person value object. This object contains only getters and setters an no logic. It represents one record in the database. I also use Hibernate Annotations instead of the XML mapping file. Using Annotations is also well supported by IntelliJ.

Person Value Object

Create Spring Services

The next step is to create the Dao classes and the Services. Adding Spring to the picture helps you to use Hibernate. Using the HibernateTemplate will make sure that you do not need to worry about connections and also reduces the amount of code you need to write.

springdao

After creating the Data Access Object we need to create the Service. The only thing our service will do is call the Dao and return the list of people.

springservice

The HTML client

Before creating our AIR application we will also create a small Spring View for the data. You can find the code for this in the PersonController class included in the sample.

The Adobe AIR client

Now comes the easy part :)

We will create an Adobe AIR application that connects directly to our Spring Service and displays the list of Person Objects in a DataGrid. If you want to directly call a object configured in Spring you will need an extra class. I have used the code for the SpringFactory from this article.

Now we just need to add this to the BlazeDS remoting-config.xml file, and then the Java app will instantiate the PersonService we created in the previous step for us:

remotingconfig

If you want to call the service from Flex you must use the RemoteObject class. The following code shows how to call the PersonService from the Flex code:

remoteobject

Conclusion

Using BlazeDS from your Flex code is very easy and I think it will be the standard way of working for us. But configuring the Server and Java application is a bit hard when you start playing with is. You can find a lot of documentation on the BlazeDS site, so that is not really a problem.

If you have any comments or suggestions please let me know.

Filed under: air, flex Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.