Christophe has posted 2 posts at DZone. View Full User Profile

An Introduction to Spring BlazeDS Integration

04.02.2009
| 152770 views |
  • submit to reddit

Accessing Spring Beans from a Flex Application

If Flex clients can remotely access Java objects using BlazeDS, and if Spring beans are just regular Java objects, aren't you all set and ready to start accessing Spring beans from Flex clients? Almost…

By default, you configure BlazeDS remote objects in a configuration file called remoting-config.xml located in WEB-INF\flex. For example, to make a Java class named ProductService available remotely to a Flex application under the logical name “product”, you would define a destination as follows:

<destination id="product">
<properties>
<source>my.package.ProductService</source>
</properties>
</destination>


This destination allows a Flex application to invoke the public methods of ProductService. Here is a minimalist Flex application that populates a DataGrid with a list of products obtained by remotely invoking the findAll() method of the ProductService class.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:RemoteObject id="ro" destination="productService"/>

<mx:DataGrid dataProvider="{ro.findAll.lastResult}" width="100%" height="100%"/>

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

</mx:Application>

 

The problem is that by default, BlazeDS takes care of instantiating and managing the life cycle of the remote objects defined in remoting-config.xml. The whole idea behind Spring IoC, however, is to let the container instantiate components (and inject their dependencies). The key to the integration of Spring and BlazeDS, therefore, is to find the best possible arrangement to leverage the data services provided by BlazeDS while letting the Spring container manage the configuration, instantiation, and life cycle of the remote objects.

The Old Approach: SpringFactory

BlazeDS provides a general purpose integration/pluggability mechanism based on the Factory pattern. You can plug a custom factory in the definition of a destination to delegate the instantiation and life cycle management of the underlying remote object to another subsystem. The role of the factory is to provide ready-to-use instances of objects to BlazeDS instead of letting BlazeDS instantiate those components. To create a factory, you create a class that implements the flex.messaging.FlexFactory interface, and you implement the lookup() method used by BlazeDS to obtain an instance of a remote object.  Internally, the lookup() method may construct a new instance, or obtain it from somewhere else.

Historically, Adobe has provided a SpringFactory class that provides BlazeDS with fully initialized (dependency-injected) instances of objects obtained from the Spring container.

Configuring a destination to delegate the instantiation of a remote object to Spring was a two step process.

1.    Register the Spring factory in WEB-INF/flex/services-config.xml:

<factories>
<factory id="spring" class="flex.samples.factories.SpringFactory"/>
</factories>


2.    Configure the destination to use the factory:

<destination id="myService">
<properties>
<factory>spring</factory>
<source>myBean</source>
</properties>
</destination>



Although it provides basic integration between BlazeDS and Spring, the factory approach has a number of shortcomings:

  • The “dependency lookup” approach described above is squarely at odds with the Spring “dependency injection” approach.
  • Objects have to be configured twice: once in the BlazeDS remoting-config.xml file and again in the Spring application context file.
  • The overall system configuration is spread over multiple files. BlazeDS is configured in web.xml, and remoting-config.xml, while Spring is configured in its own application context XML file.
  • The integration is limited to basic “remoting” and doesn’t cover other important aspects such as security and messaging.

The Spring BlazeDS Integration Project

The SpringSource and Adobe partnership is aimed at streamlining the integration between Spring and BlazeDS. This partnership has resulted in a new subproject in the Spring web portfolio: the Spring BlazeDS Integration project.

Instead of having two concurrent systems that have their own configuration approach and that communicate in a rudimentary way, the goal of the project was to let Spring manage BlazeDS “the Spring way”, just as it manages the other components of an application, and therefore provide a much deeper and cohesive level of integration between the two technologies.

The Spring BlazeDS Integration project is still a work in progress, but the currently available Milestone 2 build already fulfills that vision with the following features:

  1. The MessageBroker, which is the cornerstone of the BlazeDS engine, is configured and bootstrapped as a Spring-managed bean and doesn’t need to be configured in web.xml.
  2. Flex messages are routed to the MessageBroker through the Spring DispatcherServlet.
  3. Remote objects are configured the “Spring way” in the application context configuration file. You don’t need a remoting-config.xml file when working with BlazeDS and Spring.
  4. The Spring security integration ensures that you can secure Spring-managed beans exposed as remote objects just as you would secure any other Spring-managed endpoint, and that you can provide your security credentials from within a Flex application through the channelSet.login() API.
Published at DZone with permission of its author, Christophe Coenraets.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Roger Marin replied on Thu, 2009/04/02 - 9:56am

Awesome tutorial, any chance we can get the source code?

Jan Vissers (ak... replied on Thu, 2009/04/02 - 12:28pm

Sorry but I don't quite get what this Spring integration offers more that the old SpringFactory way. We used that about half a year ago without any shortcomings. Basically we had one (Spring security enabled) security service exposed via BlazeDS that took care of psuedo single sign-on for us. Every other top-level business module was fronted by a facade, which was exposed via a simple BlazeDS RO Service entry - much like a stateless session bean.


Ryan Heaton replied on Thu, 2009/04/02 - 8:02pm in response to: Jan Vissers (aka Pojo)

Agreed. What's the news here? We've had full Spring+BlazeDS support in Enunciate now for almost a year using the plain-ol' SpringFactory.

ismav ikathry replied on Mon, 2009/04/06 - 11:58pm

Hi christophe,

                        I am a new bee to the spring and flex, I had seen many tutorials and i understood how they work but my doubt is how do i start my own project from scratch. Like is there any basic file so that I can import into Myeclipse and start building my app?Hope you understood my problem.

 by the way thanks for your tutorials which give gave a good overall idea. 

 

Vamsi

Tom Celis replied on Fri, 2009/04/24 - 3:20am

Nice tutorial! But I got stuck on a problem... When I call a remote object from flex (for example: product) I get the following errormessage:

[RPC Fault faultString="[MessagingError message='Destination 'product' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']" faultCode="InvokeFailed" faultDetail="Couldn't establish a connection to 'product'"]

 I've configured it exactly the way you did:

...
  <flex:message-broker />
    <bean id="product" class="product.ProductDaoImpl">
        <flex:remote-service />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
        scope="singleton">
        <property name="configLocation" value="WEB-INF/hibernate.cfg.xml" />
    </bean>
...

Do you have any clue?

 

thx!

Jonny Langford replied on Fri, 2009/05/08 - 5:32am

I got this error when making my remote call:

[RPC Fault faultString="Destination 'mortgageService' not accessible over channel 'my-amf'." faultCode="Server.Processing" faultDetail="null"]

 A bit of digging and turns out you need a default channel defined in the services-config.xml (and you don't need an include for remoting-config.xml). So services-config.xml should start like :

    <services>
        <service-include file-path="proxy-config.xml" />
        <service-include file-path="messaging-config.xml" />
        <default-channels>
            <channel ref="my-amf" />
        </default-channels>
    </services>

Hope this helps..

Dima Ponomarev replied on Fri, 2009/05/29 - 11:49am in response to: Tom Celis

May be you need to define a channel in tag <flex:remote-service />.

See a working example at  http://dima-vp.livejournal.com/573.html.

Note that I'm using a newer version of Spring-Flex library so the tag looks like <flex:remoting-destination channels="my-amf"/>.

Chetan Minajagi replied on Mon, 2009/08/10 - 10:39am

Hi,

I have been to follow the steps outlined here.I have accordingly created a new webapp in the turnkey blazeds tomcat and come up with the required configurations as outlined in this tutorial.However when i startup tomcat and try to access this link like http://localhost:<port>/<my-webapp> .I get a Requested resource is not available.

 

I don't see any errors on the tomcat logs also to help me figure out where i am going wrong.

can  you please put up a downloadable of this tutorial that we can drop into the webapps folder and then follow with the instructions in this tutorial.

 

I have spent more than a day in getting this right but i have no clue abt where/what needs to be done.

 

Regards,

Chetan

Chetan Minajagi replied on Tue, 2009/08/11 - 9:29am in response to: Chetan Minajagi

Hi,

 

I have made some progress now but not entirely met with success though.

I have created a bean id by name 'greetingService' in a spring-config file and referring that file from web-application-context.xml

 

I am now facing this error

 

Error creating bean with name 'org.springframework.flex.remoting.RemotingDestinationExporter#0': Cannot resolve reference to bean 'greetingService' while setting bean property 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'greetingService' is defined

 

Can somebody help me resolve this.

 

Regards,

Chetan

 

 

 

Damaraju Vittal replied on Mon, 2009/08/24 - 6:34am

Hi ,

Create a Default channel in service-configt.xml . Add following code in 

  <default-channels>
            <channel ref="my-amf" />
        </default-channels>   

 

Vittal.

Christopher Grant replied on Mon, 2009/10/19 - 8:39am in response to: Chetan Minajagi

Hey Chetan, I had the same problem for awhile. It's because flash isn't loading the config. I don't know why some people have the issue and some don't be heres how I fixed it. The first way is to simply add an endpoint parameter to your remoteobject tag. The more correct way is to utilize the config file. To tell flash where and which config file to use, make sure your flex compiler has the -compiler.services option added for the value, point it to the physical lcoation of your service-config.xml file. This will tell flex where to lad the service config from and allow your app to locate the server/service Cheers -Christopher http://cgrant.wordpress.com

Martijn Bruinenberg replied on Fri, 2009/10/23 - 2:14am

See my Paddle generator http://paddle.devoorkant.nl.

I implemented a client-server example and a messaging service example as well.

-- Martijn

Logon Smith replied on Fri, 2010/03/05 - 11:47pm

I think the core features of the Spring Framework can be used by any Java application, but there are extensions for building web applications on top of the Java Enterprise platform.I learned these things during my exam preparation. Although the Spring Framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.

Carla Brian replied on Wed, 2012/07/18 - 8:16am

I haven't heard about this one yet. I need more resources on this. I want to learn how to do this one. - Mercy Ministries

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.