---
title: "Flex 与spring的结合"
author: "sanshi"
date: "2010-01-05T11:58:39Z"
tags: ["Bean", "Flex", "Spring", "Web", "XML"]
categories: ["Web前端"]
slug: "flex-spring"
draft: false
lastmod: "2010-01-05T11:58:39Z"
id: 562870
url: "https://sanshi.iteye.com/blog/562870"
---

与spring的结合  
   首先下载springFactory.java,内容如下：  
      
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;  
            }  
        }  
         
    }

}  
接着在services-config.xml里加入：  
    &lt;factories&gt;  
      &lt;factory id="spring" class="包名.SpringFactory"/&gt;  
 &lt;/factories&gt;  
 我们在applicationContext.xml里配置bean  
    &lt;bean id="testFactory"  class="HelloWorld"/&gt;  
 在remoting-config.xml里加入：  
     &lt;destination id="testFac"&gt;  
        &lt;properties&gt;  
         &lt;factory&gt;spring&lt;/factory&gt;  
          &lt;source&gt;testFactory&lt;/source&gt;  
        &lt;/properties&gt;  
    &lt;/destination&gt;  
后台已经配置完毕，在前台：  
    &lt;mx:RemoteObject   id = "ro"    endpoint="/TestFlex/messagebroker/amf"  destination="testFac"  result = "resultHandler(event)"/&gt;  
可以看出remoting-config.xml里配置起到了中转的作用