Integrating Flex with Spring
When I started to look at building front-end of my application in Flex, the very first challenge was the back-end integration. Being on the J2EE tech stack, it was easy because of the BlazeDS provided by Adobe itself. But, still there was challenge to integrate the application with Spring. There was another option GraniteDS which provided Spring out of the box.
I was getting inclined to BlazeDS, for the top reason that it was provided by Adobe and at a later point if we ever needed to move to LCDS, it would be easy.
Once the decision was made, the problem that loomed at large was the Spring integration that was not taken care in the BlazeDS yet. And, I struggled quite a bit to get that working for my application. Below you will find detailed steps to provide an integration between Flex and Spring. For the purposes of this demo, I am assuming that you know how to configure Spring and already have that in place.
Setup your Spring destinations
1. You need to configure a factory that can be used to fetch spring beans from the J2EE container. To do that, you need to define an entry in the services-config.xml as:
<factory id=”spring” class=”com.kapil.myapplication.flex.common.SpringFactory”/>
2. Now you have to define your destinations so that they can ask the Factory to get the bean to them. As I am using remoting (on AMF), I have configured the remoting-config.xml. The entries below state that that the destinaton “userService”, shoudl get the service from the SpringFactory
Setup Spring Factory on the container
As I said that there is not out-of-the-box integration between BlazeDS and Spring, I wrote my own implementation of the “flex.messaging.FlexFactory” interface and placed that in the classpath of the J2EE container. Various methods that you need to implement are:
public void initialize(String id, ConfigMap configMap);
You do not have to write any implementation, through you can use this for doing any kind of initialization that you may need.
public FactoryInstance createFactoryInstance(String id, ConfigMap properties);
This method is called when we initialize the definition of an instance which will be looked up by this factory. It should validate that the properties supplied are valid to define an instance. Any valid properties used for this configuration must be accessed to avoid warnings about unused configuration elements. If your factory is only used for application scoped components, this method can simply return a factory instance which delegates the creation of the component to the FactoryInstance’s lookup method.
public Object lookup(FactoryInstance inst);
Returns the instance specified by the source and properties arguments. For the factory, this may mean constructing a new instance, optionally registering it in some other name space such as the session or JNDI, and then returning it or it may mean creating a new instance and returning it. This method is called for each request to operate on the given item by the system so it should be relatively efficient.
To do the actual lookup, you need to also subclass the FactoryClass interface. The class should have the following implementation:
public Object lookup();
Is the method that has the actual code for you to find the Spring bean. You can write which ever way to load the SpringContext and then find the bean. I had used the approach of a ServiceLocator which is a singleton and helps me find the bean from the common pool of beans. You can do whatever you like to do.
Hope this helps.