环境: jdk1.6 j2ee1.5 spring2.5.6 blazeDS3.3 tomcat6.0 flex4 myeclipse8.5 flashBuilder4
步骤:一、 启动好blazeDS(即启动tomcat,在[tomcat]/webapps目录下产生一个blazeds文件夹(三个war包产生一个blazeds文件夹)); 在myeclipse8.5新建一个web Project工程,工程名为webSpring; 把此工程加入blazeDS支持(即用blazeds下的WEB-INF文件夹替换掉web工程下的WEB-INF文件夹); 加入spring支持(把spring相关的jar包拷贝到webSpring/WebRoot/WEB-INF/lib目录下即可)。
二、 1. 在javaWeb工程webSpring的str目录下分别新建一下两个包: cn.xuediit.myFactory、cn.xuediit.myService; 2. 在cn.xuediit.myFctory包下新建两个类:FlexFactoryImpl.java和SpringFactoryInstance.java (1). FlexFactoryImpl.java: package cn.xuediit.myFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import flex.messaging.FactoryInstance; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap;
public class FlexFactoryImpl implements FlexFactory { private Log log = LogFactory.getLog(getClass());
/*override interface method*/ public void initialize(String id, ConfigMap configMap) { System.out.println("1---flex工厂实现类重写的方法initialize"); }
/*override interface method*/ public FactoryInstance createFactoryInstance(String id, ConfigMap properties) { System.out.println("2---flex工厂实现类重写的方法createFactoryInstance"); log.info("Create FactoryInstance."); SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties); instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId())); return instance; }
/*override interface method*/ public Object lookup(FactoryInstance instanceInfo) { System.out.println("4---flex工厂实现类重写的方法lookup"); log.info("Lookup service object."); return instanceInfo.lookup(); }
}
(2).SpringFactoryInstance.java package cn.xuediit.myFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;
import flex.messaging.FactoryInstance; import flex.messaging.FlexContext; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap; import flex.messaging.services.ServiceException;
public class SpringFactoryInstance extends FactoryInstance { private Log log = LogFactory.getLog(getClass());
SpringFactoryInstance(FlexFactory factory, String id, ConfigMap properties) { super(factory, id, properties); }
public Object lookup() { System.out.println("3---spring工厂类的方法lookup"); ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(FlexContext.getServletConfig().getServletContext()); String beanName = getSource(); try { log.info("Lookup bean from Spring ApplicationContext: " + beanName); return appContext.getBean(beanName); } catch (NoSuchBeanDefinitionException nex) { ServiceException e = new ServiceException(); String msg = "Spring service named '" + beanName + "' does not exist."; e.setMessage(msg); e.setRootCause(nex); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } catch (BeansException bex) { ServiceException e = new ServiceException(); String msg = "Unable to create Spring service named '" + beanName + "'."; e.setMessage(msg); e.setRootCause(bex); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } catch (Exception ex) { ServiceException e = new ServiceException(); String msg = "Unexpected exception when trying to create Spring service named '" + beanName + "'."; e.setMessage(msg); e.setRootCause(ex); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } }
}
3. 在cn.xuediit.myService包下新建两个类:FService.java和FServicesImpl.java (1). FService.java package cn.xuediit.myService;
public interface FService { public String sayHello(String name); }
(2). FServicesImpl.java package cn.xuediit.myService;
public class FServicesImpl implements FService { public String sayHello(String name) { System.out.println("5---服务层实现类(本质上的与flex交互的类)"); return "我是服务层的服务实现类==" + name; }
}
三、 1、 在javaWeb工程webSpring下,在文件webSpring/WebRoot/WEB-INF/web.xml的<web-app>标签下添加子节点: <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 2、 在javaWeb工程webSpring下,在webSpring/WebRoot/WEB-INF目录下新建一个文件:applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="fServiceImplBeanID" class="cn.xuediit.myService.FServicesImpl"></bean> </beans>
四、 1、 在javaWeb工程webSpring下,在WebRoot/WEB-INF/flex/remoting-config.xml文件中的<service>标签下添加: <destination id="destinationID"> <properties> <factory>flexFactoryImplID</factory> <source>fServiceImplBeanID</source> <scope>application</scope> </properties> </destination>
2、 在javaWeb工程webSpring下,在WebRoot/WEB-INF/flex/services-config.xml文件中的<services-config>标签下添加: <factories> <factory id="flexFactoryImplID" class="cn.xuediit.myFactory.FlexFactoryImpl"/> </factories>
五、 给此javaWeb工程添加tomcat支持,启动tomcat(这个容易就不说了)。
六、 在flashBuilder下新建一个基于blazeDS的flex项目(以webSpring为后台工程),工程名为webFb; webFb.mxml: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="500" minHeight="200">
<fx:Script> <![CDATA[ import mx.core.Application; import mx.rpc.events.FaultEvent; import mx.collections.ArrayCollection; import mx.rpc.remoting.mxml.RemoteObject; import mx.controls.Alert; import mx.rpc.events.ResultEvent; public function submit(name:String):void{ var remote:RemoteObject = new RemoteObject(); remote.destination = "destinationID"; remote.endpoint = "http://localhost:8080/webSpring/messagebroker/amf"; remote.addEventListener(ResultEvent.RESULT, myResult); remote.addEventListener(FaultEvent.FAULT,fault); remote.sayHello(name); } private function myResult(evt:ResultEvent):void{ Alert.show(evt.result.toString()); } private function fault(evt:FaultEvent):void{ Alert.show(evt.fault.message); } ]]> </fx:Script>
<s:Button x="240" y="11" label="要发送到" click="submit(nameTxt.text)"/> <s:Label x="16" y="11" text="姓名"/> <s:TextInput id="nameTxt" x="100" y="100"/>
</s:Application>
七、 重启tomcat,运行flex程序,如弹出:我是服务层的服务实现类==(输入框的内容),则成功。
本文来自博客,转载请标明出处:http://blog.csdn.net/luodong224/archive/2010/11/25/6036210.aspx