Over a million developers have joined DZone.

Spring BlazeDS Integration with Flex 4 Final

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

For more than a year, SpringSource and Adobe have fostered a partnership aimed at optimizing the integration between Spring and BlazeDS.  The Spring BlazeDS Integration project succeeded in bringing Flex front ends and Spring back ends together seamlessly.  Christophe Coenraets, a technical evangelist for Adobe, gives an overview of the Spring BlazeDS integration with Flex 4, which recently went GA.  Whether you're a Spring developer that's thinking about learning Flex, or a Flex developer that wants to learn Spring, you can benefit from knowing how to integrate these best-of-breed frameworks for rich, sophisticated web applications.

BlazeDS is a powerful, server-based Java remoting and web messaging technology that enables developers to easily connect to back-end distributed data and push data in real-time to Adobe Flex and Adobe AIR.  Flex 4 can already access back end data via HTTPService (The Flex HTTPService is similar to XMLHttpRequest in Ajax) or WebService components.  With BlazeDS, Flex applications have remoting services for invoking Java object methods, message services for publishing messages and subscribing to a message destination, and proxy services that allow cross domain service requests in a secure and controlled manner.  BlazeDS is deployed as a set of JAR files within your web application.  Developers using the Spring Blaze DS integration should be aware that Spring's various modules may be harnessed in the building of remote objects as well.  

Flex Application Access to Spring Beans
Without Spring BlazeDS integration, you would need to configure BlazeDS remote objects in the remoting-config.xml file located in WEB-INF\flex to start accessing Spring beans from a Flex client .  This example shows how you would expose the ProductService Java class to remote availability for a Flex application:
<destination id="product">
<properties>
<source>my.package.ProductService</source>
</properties>
</destination>
The logical name used is "product" and the destination allows Flex to invoke the public methods of ProductService.  Here is Christophe Coenraets's minimalist Flex application used to populate a DataGrid with a product list.  The list is procured by remotely invoking the findAll() method of the ProductService class:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<s:RemoteObject id="ro" destination="productService"/>
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:DataGrid dataProvider="{ro.findAll.lastResult}" />
<s:Button label="Get Data" click="ro.findAll()"/>

</s:Application>
BlazeDS instantiates and manages the life cycles of the remote objects defined in the remoting-config.xml file, but this is counter to Spring's methodology, which lets the container instantiate components.  The trick is to find the best arrangement when integrating Spring and BlazeDS so that you can leverage the data services of BlazeDS while letting the Spring container manage the config, instantiation, and life cycles of the remote objects.  Still, the Spring BlazeDS integration projects has a much better way of doing things.

Spring Factory: the old way to integrate
BlazeDS is an integration mechanism based on the Factory pattern, meaning you can plug a custom factory into a destination's definition to let another subsystem manage the remote object's instantiation and life cycle.  The factory approach has some disadvantages which lead to the Spring BlazeDS integration: The dependency lookup approach runs counter to the Spring DI approach, the objects have to be configured twice, the overall system config is dispersed over many files, and integration is only possible through basic remoting without security and messaging.

Spring BlazeDS Integration
The Spring Blaze DS integration project is aimed at removing some of these disadvantages in running a Flex client with a Spring back end.  The project makes BlazeDS cooperate with Spring's management paradigms.  The MessageBroker in BlazeDS is configured and bootstrapped as a Spring-managed bean requiring no web.xml configuration.  Flex messages are routed through the Spring DispatcherServlet to the MessageBroker.  Remote objects are configured in the application context config file.  Messaging destinations are configured in the "Spring Way", and Spring security integration lets you provide security credentials from within a Flex app and secures Spring-managed beans exposed as remote objects just like you would in Spring.

After you configure Spring in web.xml, configure the BlazeDS message broker as a Spring-managed bean in the application context configuration file, configured your beans, and then expose the beans as remote objects in the application context config file, then you can get started with a simple example shown below.

First, configure the DispatcherServlet to bootstrap the Spring WebApplicationContext in web.xml.  Next, you map all the /messagebroker requests to the DispatcherServlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>

</web-app>
Within the web-application-config.xml, the BlazeDS message broker is first configured as a Spring-managed bean using the simple message-broker tag. This bootstraps the BlazeDS message broker. Using the message-broker tag without mapping child elements causes all incoming DispatcherServlet requests to be mapped to the MessageBroker, but you can certainly add mapping child elements for more control.

Once you're finished with the message broker, you can configure your Spring beans normally and expose the beans you want to make available for remote access using the remoting-destination tag.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

<flex:message-broker/>

<bean id="productService" class="org.springframework.flex.samples.product.ProductDAO" >
<flex:remoting-destination />
<constructor-arg ref="dataSource" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/sprinflexdemodb/sprinflexdemodb" />
</bean>
</beans>
With this config, the Flex application can invoke the productService bean's public methods. The Flex application below populates a DataGrid with a list of products.  The products are acquired by remotely invoking the findAll() method of the ProductService class.
 <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Declarations>
<s:RemoteObject id="ro" destination="productService"/>
</fx:Declarations>

<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:DataGrid dataProvider="{ro.findAll.lastResult}" />

<s:Button label="Get Data" click="ro.findAll()"/>

</s:Application>
This is the same code that was used to connect to a plain BlazeDS installation earlier in this article!  The client-side code and the specific server-side implementation are isolated from one another.

Spring developers can easily leverage their existing infrastructure and expose the beans to BlazeDS remoting for remote access by Flex clients.  Flex users can use the BlazeDS integration to harness the powerful features of the Spring Framework.  

For more information on other features like messaging and security integration, read Spring BlazeDS Integration Test Drive.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}