(1) 在官网上下载blazeds的开发包,最好下载完整版,里面有一个例子和所有我们所需的所有文件(我下载的是blazeds-turnkey-4.0.0.14931版)。将blazeds.war、ds-console.war、samples.war三个文件放在tomcat的webapps目录下
flex-messaging-common.jar
flex-messaging-core.jar
flex-messaging-opt.jar
flex-messaging-proxy.jar
flex-messaging-remoting.jar
backport-util-concurrent.jar
cfgatewayadapter.jar
commons-httpclient-3.0.1.jar
commons-codec-1.3.jar
commons-logging.jar
concurrent.jar
xalan.jar
(2)然后要加入Flex BlazeDS需要的配置文件。在WEB-INF下新建一个名为flex的文件夹,然后将四个xml文件(messagin-config.xml/proxy-config.xml/remoting-config.xml/services-config.xml)拷到该文件夹下。
(3) 修改web.xml文件,加入Flex的配置(直接拷贝即可)。
<context-param>
<param-name>flex.class.path</param-name>
<param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>
</context-param>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
以上步骤需在java项目中完成
(1) 在Flex Builder中新建一J2EE项目,并配置服务器路径(在tomcat/webapp/目录下,需关联一web项目)
(2) 在Flex Build Path中引入Cairngorm.swc,查看是否引用了正确的sdk包,如不正确,需重新引用。
(3) 按照Cairngorm架构,在src目录下分别创建bussiness,command,event,model,view文件夹。
其中,view为视图层,model为模型层,event为事件,command为业务逻辑层,bussiness中需创建delegate(中间人角色,command调用web service获得数据时,需创建一个delegate完成,一对一关系),Front Controller(注册event和command的对应关系)和services.mxml(服务定义)。
具体调用过程如下:
n 首先我们要了解我们的数据
n 我们将使用数据来定义View
n 我们将使用View来定义可能的用户动作
n 用户动作将会转化(转变)为Events
n Event将会被映射到Command
n Command可能被映射到Delegate
n Delegate映射到Service
n Command将使用Service传回的结果更新Model Locator
3. Flex和Spring整合
public class SpringFactory implements FlexFactory{
//定义一个常量资源
private static final String SOURCE = "source";
public void initialize(String id, ConfigMap configMap) {
}
/**
* 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.
*/
//创建一个factory实例
public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
//生成一个Spring的实例
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
}
public Object lookup(FactoryInstance inst)
{
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
return factoryInstance.lookup();
}
//内部静态类
static class SpringFactoryInstance extends FactoryInstance
{
//内部类构造函数
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
{
super(factory, id, properties);
}
//用于测试
public String toString()
{
return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
}
//查询
public Object lookup()
{
//这就是从spring容器中getbean了
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try
{
return appContext.getBean(beanName);
}
catch (NoSuchBeanDefinitionException nexc)
{
ServiceException e = new ServiceException();
String msg = "Spring service named '" + beanName + "' does not exist.";
e.setMessage(msg);
e.setRootCause(nexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
catch (BeansException bexc)
{
ServiceException e = new ServiceException();
String msg = "Unable to create Spring service named '" + beanName + "' ";
e.setMessage(msg);
e.setRootCause(bexc);
e.setDetails(msg);
e.setCode("Server.Processing");
throw e;
}
}
}
(2) 在Spring配置文件中注册相应flex实现接口(跟正常配置spring一样)
(3) 在Services-config.xml中加入如下代码:
<factories>
<factory id="spring"class="flexHandle.SpringFactory"/>
</factories>
指定flex加载bean的工厂类的路径
(4) 最后,在remoting-config.xml中加入如下代码:
<destination id="Client2Server">
<properties>
<factory>spring</factory>//Flex工厂类
<source>clienToServerManager</source>//Flex接口bean
</properties>
</destination>
参考文档:《整合Flex和Java—配置篇》
《基于Cairngorm的Flex应用程序设计》
参考网站:http://blog.dreamhui.net/archives/64
http://www.iteye.com/topic/180613
http://www.oschina.net/question/12_7654