-

Twitter
- Blogged: "FlexStore Revisited: Building an Animated Spark Layout" http://bit.ly/96JOa7 #Flex4
- Blogged "More #Flex4 / Spark ItemRenderer Samples" http://bit.ly/c5wcGF
- Principal Flex UI Engineer/Architect position at Attivio, a great startup in the Boston Area. http://www.attivio.com/attivio/careers.html
- Blogged: "Cool ItemRenderers Made Easy in Flex 4" http://bit.ly/bnyGwI
- Blogged "Creating a Custom Component and Skins in Flex 4" http://bit.ly/8Hng9b
Archives
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- September 2008
- May 2008
- April 2008
- February 2008
- December 2007
- November 2007
- October 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- February 2007
- January 2007
- November 2006
- October 2006
- September 2006
- August 2006
- June 2006
Categories

Using Flex with Spring
UPDATE (1/27/2008): The approach described in this article is now obsolete. Adobe and SpringSource are now collaborating on an open source project called: Spring BlazeDS integration. You can read more about the project and download the bits here: http://www.springsource.org/spring-flex. I also wrote a Test Drive providing out-of-the-box examples using this project. The test drive is available here.
UPDATE (1/12/2007): I put together a Tomcat-based Test Drive Server that includes the samples described below running out-of-the box. Read this post for more info.
What is Spring?
Spring is one of the most popular Java frameworks. The foundation of the Spring framework is a lightweight component container that implements the Inversion of Control (IoC) pattern.
Using an IoC container, components don’t instantiate or even look up their dependencies (the objects they work with). The container is responsible for injecting those dependencies when it creates the components (hence the term “Dependency Injection” also used to describe this pattern).
The result is looser coupling between components. The Spring IoC container has proven to be a solid foundation for building robust enterprise applications. The components managed by the Spring IoC container are called Spring beans.
The Spring framework includes several other modules in addition to its core IoC container. These modules are not covered in this document even though we will be using the Spring JDBC abstraction framework in examples 2 and 3 below. More information on the Spring framework can be found at http://www.springframework.org.
What is Flex?
Flex provides a complete solution for building Rich Internet Applications. The Flex programming model is made of:
The Flex source code (.mxml and .as files) is compiled into Flash bytecode (.swf) that is executed at the client-side by the Flash virtual machine using a Just-In-Time compiler.
A complete discussion of Flex is beyond the scope of this document. You can find more information at http://www.adobe.com/products/flex.
How does Flex access back-end systems?
When writing Flex applications, you can access back-end systems using four different strategies:
In this document, we focus on the Remoting (3) and Data Management Services (4) approaches described above because they enable the tightest integration with Spring. There is no need to transform data, or to expose services in a certain way: the Flex application works directly with the beans registered in the Spring IoC container.
How does Flex access Spring beans?
So, if Flex clients can remotely access Java objects, and if Spring beans are Java objects, aren’t we all set and ready to start accessing Spring beans from Flex clients? Almost… There is one simple element to configure.
The whole idea behind Spring IoC is to let the container instantiate components (and inject their dependencies). By default, however, components accessed remotely by a Flex client are instantiated by Flex destinations at the server-side. The key to the Flex/Spring integration is therefore to configure the Flex destinations to let the Spring container take care of instantiating Spring beans. The Flex Data Services support the concept of factory to enable this type of custom component instantiation. The role of a factory is simply to provide ready-to-use instances of components to a Flex destination (instead of letting the Flex destination instantiate these components itself).
The supporting files available with this document include a factory class (SpringFactory) that provides Flex destinations with fully initialized (dependency-injected) instances of Spring beans. Note: The SpringFactory was developed by Jeff Vroom (Flex Data Services architect) and is also available on Adobe Exchange.
The remaining of this article describes how to configure your web application to use Flex and Spring, how to configure the Spring Factory, and how to put the pieces together and start invoking Spring beans from Flex applications.
Setting Up your Web Application to Use Flex and Spring
Step 1: Install the supporting files
flex-spring.zip includes the Spring factory as well as the supporting files for the examples below.
Step 2: Install Flex Data Services
To use the Remoting and Data Management Services data access strategies described above, you need to install the Flex Data Services. If you haven’t already done so, you can download the Flex Data Services here, and follow the installation instructions.
The installation process will install three web applications (flex, samples, and flex-admin). You can use either the flex or samples web application to run the examples below.
You can read more information on the installation process here: http://www.adobe.com/support/documentation/en/flex/2/install.html#installingfds2
Step 3: Install Spring
Note: A complete discussion of the Spring installation process is beyond the scope of this article. Refer to http://www.springframework.org for more information. The steps below describe a basic configuration that is sufficient for the purpose of this article.
Note: The examples below have been developed and tested using Spring 2.0. However the integration approach described in this document (and the SpringFactory class) should work fine using Spring 1.2.8 (some of the examples might not work because they use Spring 2.0 features).
[/xml] <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </pre> </li> </ol> <h3>Step 4: Register the Spring Factory</h3> <ol> <li>Copy SpringFactory.class and SpringFactory$SpringFactoryInstance.class from flex-spring-sdk\bin\flex\samples\factories to {context-root}\WEB-INF\classes\flex\samples\factories</li> <li>Register the Spring factory in {context-root}\WEB-INF\flex\services-config.xml: [xml]Example 1: Mortgage Calculator Using Flex Remoting
This first application is intentionally simplistic in its functionality to provide an uncluttered example of wiring Spring beans together and invoking them from a Flex application.
Step 1: Copy the application files
Step 2: Register Spring Beans
[/xml] <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="rateFinderBean" class="flex.samples.spring.mortgage.SimpleRateFinder"/> <bean id="mortgageBean" class="flex.samples.spring.mortgage.Mortgage"> <property name="rateFinder" ref="rateFinderBean"/> </bean> </beans> </pre> Notice that in the mortgageBean definition, we tell the container how to inject the rateFinder dependency: the rateFinder property is mapped to rateFinderBean which defines an instance of the SimpleRateFinder class. </li></ol> <h3>Step 3: Configure the Flex Remoting Destination</h3> <ol> <li>Open remoting-config.xml in {context-root}\WEB-INF\flex.</li> <li>Add a mortgageService destination as follows: [xml]Notice that we use the spring factory defined above (see “Register the Spring Factory”), and we provide the name of the Spring bean as defined in applicationContext.xml as the source.
Step 4: Run the Client Application
Note: that there is a delay the first time you access an application in this manner. This is because we are using the web compiler which compiles your application into bytecode the first time it is accessed (similar to the JSP compilation model). Subsequent requests to the same application will be much faster since the application is already compiled. In a production environment, you would typically deploy applications that have already been compiled using the Flex compiler available as a command-line utility or fully integrated in FlexBuilder (the Eclipse-based development environment for Flex).
Depending on your configuration, you may need to increase the heap size of your application server's JVM to use the web compiler. This would not be required in a production environment since you typically don't use the web compiler. If you get a java.lang.OutOfMemoryError exception while trying to access a sample for the first time, you must increase your heap size. Alternatively, you can compile the application using FlexBuilder or the command line compiler.
Example 2: Store/Inventory Management using Flex Remoting
This second example is more sophisticated and includes database connectivity. To keep the application simple and avoid dependencies on other products or frameworks, the Spring JDBC abstraction framework is used to access the database. You could use the Spring support for ORM data access (using Hibernate, JDO, Oracle TopLink, iBATIS, or JPA) as an alternative: the specific Spring data access strategy you choose has no impact on the Flex/Spring integration. We use an embedded HSQLDB database: the only installation requirement is to copy the HSQLDB driver in your web application classpath (see below). The application has two modules: a database maintenance module, and a customer-facing product catalog with filtering capabilities.
Step 1: Copy the application files
Step 2: Register Spring Beans
[/xml] <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:/flex-spring-sdk/db/store"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <bean id="productDAOBean" class="flex.samples.spring.store.SimpleProductDAO"> <property name="dataSource" ref="dataSource"/> </bean> </pre> Note: If you didn't unzip flex-spring-sdk in your root directory, adjust the JDBC url accordingly. </li> </ol> <h3>Step 3: Configure the Flex Remoting Destination</h3> <ol> <li>Open remoting-config.xml in {context-root}\WEB-INF\flex.</li> <li>Add the productService destination as follows: [xml]Step 4: Run the Client Application
Test the database maintenance module:
Test the store module:
Example 3: Data Management Services
In addition to the RPC services used in examples 1 and 2 above, the Flex Data Management Services provide an innovative and very productive approach to synchronize data between the client and the middle-tier. The Flex Data Management Services consist of a client-side API and server-side services:
This application provides an example of using the Flex Data Management Services with the Spring IoC container.
Note: The Flex Data Management Services leverage the Java Transaction API (JTA). If you are using Tomcat, or another servlet container that doesn't provide a full implementation of the J2EE stack, you have to install a JTA implementation such as JOTM to run this example. Click here for more information.
Step 1: Copy the application files
Step 2: Register Spring Beans
[/xml] <bean id="productAssemblerBean" class="flex.samples.spring.store.ProductAssembler"> <property name="productDAO" ref="productDAOBean"/> </bean> </pre> </li> </ol> <h3>Step 3: Configure the Flex Data Management Services Destination</h3> <ol> <li>Open data-management-config.xml in {context-root}\WEB-INF\flex.</li> <li>Add the product destination as follows: [xml]Step 4: Run the Client Application