Nov 23, 2009

Flex and Spring Integration Architecture

Many applications are implemented with a three-tier architecture wherein data is stored in the database. The web server runs Java services that access the database and retrieve the information. The Java classes are responsible for the business logic that receives a call from the client tier, assembles the information from the database, and returns the result.

The client tier utilizes browser technology that runs a Flash application to provide an interface to the user and calls the Java services for required information. The client workstations call the Tomcat server, which calls the database to gather data and executes the Spring services that contain the application’s business logic.

In this architecture, the compiled Flex and Spring code resides on the Tomcat server. The client workstations download the Flex SWF and associated files, which contain the hooks into the Spring services.

Sample Application

To demonstrate the Spring and Flex integration, I am presenting a sample application which is a web based application with the following architecture.

Spring

Spring addresses Java/Java EE development by organizing your middle-tier objects and takes care of the plumbing that is usually left up for you to create. Spring can also work in any architecture layer while running in any runtime environment.

Spring Bean Wiring

The IoC container, also called the core container, wires Spring beans together. It is also responsible

for the configuration and creation of objects in Spring.

In most cases, when an object needs references to data, it does a lookup or retrieval from an external data repository. IoC allows the component to not require information regarding the location of the data source, thus cleaning up the process of data retrieval through inverting the direction of the retrieval.

IoC helps to create layers of abstraction through your Spring applications. Spring’s core container provides a central location for accessing objects, called the application context. The application context can be configured as Java 5 annotations or as an XML file that contains the signatures of each bean that is created in a Spring application.

Building Spring Service

To demonstrate I will present a simple spring service that gets a list of objects from the database using Hibernate as the ORM technology.

Building a Simple Bean

The first item of interest is to build a bean for our service. This bean is part of the IoC container.

Beans defined in the IoC container are nothing more than a pool of components you have access to by referencing the service with which they are associated.

The MyService is a simple POJO with a method to print the project list. This bean does not have a constructor, as no arguments need to be passed to the bean at instantiation.

The Spring Framework provides a layer of abstraction for transaction management to allow you to consistently program across different transaction APIs, such as JDBC, Hibernate, JPA, and Java Data Objects (JDO). Spring provides support for both programmatic and declarative transaction management.

Once done we can now start registering the beans in the Spring ICO container called applicationContext.xml.

For simplicity, I am going to keep the database/hibernate related properties in a separate file called hibernate.properties. This has advantage that in future if the database needs to change I can change the settings in a file without touching rest of the application.

To expose the Spring services to the Flex, we need to have the BlazeDS integration done.

For this download the BlazeDS jars and Spring SBI jars and put it in your application classpath.

The SBI suite is designed as a best practice solution for integrating Flex and Spring. Its main goal is to simplify communication between Flex and Spring by providing Remoting and messaging capabilities in combination with BlazeDS.

Setting Up the BlazeDS MessageBroker

The MessageBroker is a the SBI component responsible for handling HTTP messages from the

Flex client. The MessageBroker is managed by Spring instead of BlazeDS. Messages are routed

to the Spring-managed MessageBroker via the Spring DispatcherServlet.

Lets modify the web.xml with the configuration required to handle the requests from Flex.