`
可爱の小猪
  • 浏览: 104890 次
  • 性别: Icon_minigender_1
  • 来自: 南充
社区版块
存档分类
最新评论

FLEX和Spring、Hibernate的集成 - Flex+Spring

阅读更多

remoting-config.xml:
<destination id="TestSpring">
        <properties>
            <factory>spring</factory>
            <source>springTest</source> <!--此处注入IOC注入-->
        </properties>
    </destination>

==============================================================
services-config.xml:
    <factories>
      <factory id="spring" class="flex.samples.factories.SpringFactory" />
    </factories>
==============================================================
web.xml: 和struts整合spring一样
    <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>
============================================================

applicationContext.xml

 

 <bean id="springTest" class="flex.samples.SpringTest">
  <property name="description">
   <value>Hello Flex To Spring</value>
  </property>
 </bean>


SpringFactory.java文件

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) {}

    public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
    {
        SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
        instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
        return instance;
    }
    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;
            }
        }
       
    }
}

=============================================
SpringTest.java

package flex.samples;

public class SpringTest {

 private String description;

 public String getDescription() {
  return "This description from String. [" + description + "]";
 }

 public void setDescription(String description) {
  this.description = description;
 }
}

Flex.mxml:

<mx:RemoteObject id="myspring" destination="TestSpring" />
<mx:Button x="25" y="80" label="Call Spring" width="100" click="myspring.getDescription();" />
<mx:Label x="25" y="100" width="334" text="{myspring.getDescription.lastResult}"/>

//注意颜色对应关系
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics