Spring和Flex之间通信的关键
近期由于需要做一个实时仪表盘的动态图表,于是开始研究Flex技术。
匆忙下载了FlexBuiler4,BlazeDS 3.2(用于和Java通讯),本机的Java开发环境为myeclipse6.0.1,eclipse 3.3。
在未使用Spring架构测试过程中一切都很顺利,Flex很容易就发现Web程序/WEB-INF/flex/remoting-config.xml中定义的服务。
<destination id="dataService"><properties><source>sample.service.DataService</source></properties></destination>
package flex.samples.factories;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) {} /** * 创建Flex引用的实例 **/ 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(); } /** * 通过Bean Id查找上下文取得对应Bean的实例 */ 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; } } } }
<factories> <factory id="spring" /></factories>
<beans> <bean name="dataService" singleton="true"/> </beans>
<destination id="dataService"> <properties> <factory>spring</factory> <source>dataService</source> </properties> </destination>
<mx:RemoteObject id="dataService" destination="dataService"/>
<mx:Script><![CDATA[protected function clickMe(event:MouseEvent):void{dataService.getRequest(dataRequest.text);dataService.addEventListener(ResultEvent.RESULT,getResult);}protected function getResult(event:ResultEvent):void{var s:String= event.result as String;trace(s);}]]></mx:Script><mx:TextInput id="dataRequest"/><mx:Button label="Request" click="clickMe(event)" />