Flex的的對象傳輸方式可謂簡單又方便,spring和flex又是如何連接,srping用到的jar包大致如下:org.springframework.beans, org.springframework.context,和org.springframework.web ,flex是blazeds.war。
springFactory.java文件:
package com.iteye.factories; import flex.messaging.FactoryInstance; import flex.messaging.FlexContext; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap; import flex.messaging.services.ServiceException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; @SuppressWarnings("unused") public class SpringFactory implements FlexFactory { private static final String SOURCE = "source"; public void initialize(String identifier, ConfigMap configMap) { } public FactoryInstance createFactoryInstance(String identifier, ConfigMap properties) { SpringFactoryInstance instance = new SpringFactoryInstance(this, identifier, properties); instance.setSource(properties.getPropertyAsString("source", instance .getId())); return instance; } public Object lookup(FactoryInstance instance) { return ((SpringFactoryInstance) instance).lookup(); } static class SpringFactoryInstance extends FactoryInstance { private static final String SERVER_PROCESSING = "Server.Processing"; SpringFactoryInstance(SpringFactory factory, String identifier, ConfigMap properties) { super(factory, identifier, properties); } public String toString() { return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope(); } public Object lookup() { ServiceException exception; String msg; ApplicationContext appContext = WebApplicationContextUtils .getWebApplicationContext(FlexContext.getServletConfig() .getServletContext()); String beanName = getSource(); try { return appContext.getBean(beanName); } catch (NoSuchBeanDefinitionException nexc) { exception = new ServiceException(); msg = "Spring service named '" + beanName + "' does not exist."; exception.setMessage(msg); exception.setRootCause(nexc); exception.setDetails(msg); exception.setCode("Server.Processing"); throw exception; } catch (BeansException bexc) { exception = new ServiceException(); msg = "Unable to create Spring service named '" + beanName + "' "; exception.setMessage(msg); exception.setRootCause(bexc); exception.setDetails(msg); exception.setCode("Server.Processing"); throw exception; } } } }
web.xml配置:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <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>
applicationContext.xml測試配置:
<bean id="testDAO" class="com.iteye.testDAO"> <property name="sessionFactory" ref="mySessionFactory" /> </bean>
flex下services-config.xml配置:
<factories> <factory id="spring" class="com.pega.factories.SpringFactory"/> </factories>
對象傳輸方式用到的remoting-config.xml配置:
<destination id="test"> <properties> <factory>spring</factory> <source>testDAO</source> </properties> </destination>
然後寫好這個test類,在mxml代碼中調用remoteObject組件調用這個類了。