工程需要整合Spring和Flex,在网上众多方法中找到了下面这种,记录留存。

个人认为该方法更适合在已有Spring框架的工程中添加Flex时使用,对原工程内容(主要指配置文件)改动较小。

1.添加Spring,在web.xml文件里添加配置

<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>

2.在flex的services-config.xml文件里添加Spring工厂

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

3.在Spring里面定义bean

<bean id="testClass" class="com.....TestService">
<property name="dataSource" ref="dataSource"/>
</bean>

4.在flex的远程对象配置文件里配置远程对象,这里要指定远程对象的创建工厂为第2步配置的工厂,远程对象的source要指向Spring里定义的bean

<destination id="test">
<properties>
<factory>spring</factory>
<source>testClass</source>
</properties>
</destination>

5.在页面就可以使用远程对象了

private function send():void{

    test.doSomething(...);

}

附:需要的SpringFactory

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException; public class SpringFactory implements FlexFactory
{
private static final String SOURCE = "source"; public void initialize(String id, ConfigMap configMap) {} public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
{
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
return instance;
} // end method createFactoryInstance() 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()
{
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;
}
} } }

最新文章

  1. sql 批量更新某个字段的值
  2. MongoDB常用命令
  3. Sqlserver通过链接服务器访问Oracle的那些事儿
  4. 第一个ruby程序
  5. testng+reportng,运行xml
  6. 【读书笔记】iOS网络-三种错误
  7. 你不知道的JavaScript--DOM基础详解2
  8. hdoj 1234 开门人和关门人
  9. Python之路第十三天,高级(7)-详述数据库一对多,多对多表关系的设计以及如何查询
  10. JavaScript的一点简介(注:本文诸多观点源于JavaScript高级程序设计,如有侵权,立即删除)
  11. VS2010/MFC对话框:颜色对话框
  12. hdu2837数论
  13. iOS NSTimer
  14. HA高可用集群
  15. STM32之呼吸灯实验
  16. linux下分割和重组文件
  17. JavaScript中对象数组 根据某个属性值进行排序
  18. 正则表达式和re模块
  19. win10只有edge浏览器能上网的解决方法
  20. Rewrite JSON project with Fetch

热门文章

  1. jquery append 动态添加的元素事件on 不起作用的解决方案
  2. mongoengine连接错误:“False is not a read preference”解决方法
  3. Three-Color Flag
  4. Android02-Activity01
  5. Linux系统重启network服务失败
  6. 客户端持久化解决方案: Web SQL
  7. PMC
  8. Delphi多线程数据库查询(ADO)
  9. Struts2之自定义局部类型转换器、全局类型转换器
  10. Android UI ActionBar功能-Action Bar 左上角的向上或返回按钮