1. What is Spring? Why do we care about Spring?
Christophe Coenraets ‘ s 30 minute test drive provides good answer to these questions. See details here.
2. Spring Configuration
You can follow the steps described by Christophe or Jeff Vroom, or you can follow the steps below which provide easier access to the required jars.
1). Download spring.jar and flex-spring-factory.jar to your machine, and put them into your flex app’s WEB-INF/lib directory.
2). Add the following into your flex app’s WEB-INF/web.xml file, inside <web-app> tag.
<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>
3). Add the following into to WEB-INF\flex\services-config.xml inside <services-config> tag:
<factories>
<factory id="spring" class="flex.samples.factories.SpringFactory"/>
</factories>
4). If you are using JRun as app server, set the following in WEB-INF\jrun-web.xml to avoid parser conflicting:
<jrun-web-app>
<load-system-classes-first>
true</load-system-classes-first>
</jrun-web-app>
Now you are ready to use flex to access Spring beans.
3. Create Spring beans
We are going to use the sample code in singleManagedObject.zip. To understand the code and configuration, see ""How to get Single Managed Objects from a specified remote destination". In that example, we access data from DataService using an Assembler MyAssembler.java. With the Assembler, we should be able to access Spring beans, right? Well, we are just a couple of steps away.
1). Create an interface (MyDAO.java)
Because we are trying to access Spring beans, we need to add an interface that represents all the methods we are going to use. From MyService.java we can see that there are only two methods for this sample, so MyDAO.java is going to be very simple:
package myfds;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
public interface MyDAO {
public List getProducts() throws DataAccessException ;
public MyBean getProduct(int myid) throws DataAccessException ;
}
2). Modify MyService.java and MyAssembler.java
— copy MyService.java and save it as MyServiceSpring.java
Change the class to extends JdbcDaoSupport and implements the interface:
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class MyServiceSpring extends JdbcDaoSupport implements MyDAO {
……
— copy MyAssembler.java and save it as MyAssemblerSpring.java
Change the class with setMyDAO():
import flex.data.assemblers.AbstractAssembler;
public class MyAssemblerSpring extends AbstractAssembler {
private MyDAO myDAO;
public void setMyDAO(MyDAO myDAO) {
this.myDAO = myDAO;
}
……..
3). Compile your java code
Remember to include spring.jar and flex-messaging.jar in the classpath,
C:\flex\jrun4\servers\default\samples2\WEB-INF\classes>javac -classpath ./;C:\fl
ex\jrun4\servers\default\samples2\WEB-INF\lib\spring.jar;C:\flex\jrun4\servers\d
efault\samples2\WEB-INF\lib\flex-messaging.jar -Xlint myfds/*.java
Now we have the spring bean that we can access from flex, we need to register it with our flex app, and configure flex destination to point to the Spring factory.
4. Spring beans registration and flex destination configuration
1). Create applicationContext.xml and put it under your flex app’s WEB-INF directory. Here is the applicationContext.xml for this example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!–define a data source to connect to your database –>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost/flexdemodb"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<!– define your service (methods) with a reference to dataSource –>
<bean id="myServiceSpring" class="myfds.MyServiceSpring">
<property name="dataSource" ref="dataSource"/>
</bean>
<!– define assembler with reference (dependency) to myServiceSpring –>
<bean id="myAssemblerSpring" class="myfds.MyAssemblerSpring" singleton="true">
<!–the property name myDAO should match the variable for interface MyDAO defined in MyAssemblerSpring–>
<property name="myDAO" ref="myServiceSpring"/>
</bean>
</beans>
Note, myServiceSpring will be the soruce name you use in the destination you define below.
2). Configure destination
We have registered Spring factory in WEB-INF\flex\services-config.xml in step 2 #3).
<factory id="spring" class="flex.samples.factories.SpringFactory"/>
Therefore we need to point the destination to spring factory. Add the following into WEF-INF\flex\Data-management-config.xml:
<destination id="myfds-spring">
<adapter ref="java-dao" />
<properties>
<factory>spring</factory>
<source>myAssemblerSpring</source>
<metadata>
<identity property="myid"/>
</metadata>
</properties>
</destination>
Now, in flex mxml page, you can access the destination myfds-spring by doing something like this:
<mx:DataService id="ds" destination="myfds-spring" result="getData1(event)"/>
And call the methods in the Spring beans by using ds.getItem({myid:2})
If you run myDataServiceSpring.mxml, you can get data from different method by click on different button.
Here is the simpleSpring.zip which incdules all the code.