Flex+Spring配置

Flex+Spring配置

关键字: flex spring

    flex拓扑图制作只是一个简单的例子,可以运行,只是简单的呈现部分,如果要实现具体的拓扑图的功能,需要连接数据库,这里只简单说说flex与spring的结合,有了spring,再与hibernate以及struts就简单多了。
     1、首先是web.xml需要添加flex的配置,增加如下的内容:
     
Java代码 复制代码
  1. <listener>   
  2.         <listener-class>flex.messaging.HttpFlexSession</listener-class>   
  3.     </listener>    
  4.        
  5.     <!-- MessageBroker Servlet -->   
  6.     <servlet>   
  7.         <servlet-name>MessageBrokerServlet</servlet-name>   
  8.         <display-name>MessageBrokerServlet</display-name>   
  9.         <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>   
  10.         <init-param>   
  11.             <param-name>services.configuration.file</param-name>   
  12.             <param-value>/WEB-INF/flex/services-config.xml</param-value>   
  13.        </init-param>   
  14.         <init-param>   
  15.             <param-name>flex.write.path</param-name>   
  16.             <param-value>/WEB-INF/flex</param-value>   
  17.         </init-param>   
  18.         <load-on-startup>1</load-on-startup>   
  19.     </servlet>   
  20.   
  21.     <servlet>   
  22.         <servlet-name>FlexMxmlServlet</servlet-name>   
  23.         <display-name>MXML Processor</display-name>   
  24.         <description>Servlet wrapper for the Mxml Compiler</description>   
  25.         <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>   
  26.         <init-param>   
  27.             <param-name>servlet.class</param-name>   
  28.             <param-value>flex.webtier.server.j2ee.MxmlServlet</param-value>   
  29.         </init-param>   
  30.         <init-param>   
  31.             <param-name>webtier.configuration.file</param-name>   
  32.             <param-value>/WEB-INF/flex/flex-webtier-config.xml</param-value>   
  33.         </init-param>   
  34.        <load-on-startup>1</load-on-startup>   
  35.     </servlet>   
  36.   
  37.     <servlet>   
  38.         <servlet-name>FlexSwfServlet</servlet-name>   
  39.         <display-name>SWF Retriever</display-name>   
  40.         <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>   
  41.         <init-param>   
  42.             <param-name>servlet.class</param-name>   
  43.             <param-value>flex.webtier.server.j2ee.SwfServlet</param-value>   
  44.         </init-param>   
  45.         <!-- SwfServlet must be initialized after MxmlServlet -->   
  46.         <load-on-startup>2</load-on-startup>   
  47.     </servlet>   
  48.   
  49.     <servlet>   
  50.         <servlet-name>FlexForbiddenServlet</servlet-name>   
  51.         <display-name>Prevents access to *.as/*.swc files</display-name>   
  52.         <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>   
  53.         <init-param>   
  54.             <param-name>servlet.class</param-name>   
  55.             <param-value>flex.webtier.server.j2ee.ForbiddenServlet</param-value>   
  56.         </init-param>   
  57.     </servlet>   
  58.   
  59.     <servlet>   
  60.         <servlet-name>FlexInternalServlet</servlet-name>   
  61.         <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>   
  62.         <init-param>   
  63.             <param-name>servlet.class</param-name>   
  64.             <param-value>flex.webtier.server.j2ee.filemanager.FileManagerServlet</param-value>   
  65.         </init-param>   
  66.         <load-on-startup>10</load-on-startup>   
  67.     </servlet>   
  68.   
  69.     <servlet-mapping>   
  70.         <servlet-name>MessageBrokerServlet</servlet-name>   
  71.         <url-pattern>/messagebroker/*</url-pattern>   
  72.     </servlet-mapping>   
  73.   
  74.     <servlet-mapping>   
  75.         <servlet-name>FlexMxmlServlet</servlet-name>   
  76.         <url-pattern>*.mxml</url-pattern>   
  77.     </servlet-mapping>   
  78.   
  79.     <servlet-mapping>   
  80.         <servlet-name>FlexSwfServlet</servlet-name>   
  81.         <url-pattern>*.swf</url-pattern>   
  82.     </servlet-mapping>   
  83.   
  84.     <servlet-mapping>   
  85.         <servlet-name>FlexForbiddenServlet</servlet-name>   
  86.         <url-pattern>*.as</url-pattern>   
  87.     </servlet-mapping>   
  88.   
  89.     <servlet-mapping>   
  90.         <servlet-name>FlexForbiddenServlet</servlet-name>   
  91.         <url-pattern>*.swc</url-pattern>   
  92.     </servlet-mapping>   
  93.   
  94.     <servlet-mapping>   
  95.         <servlet-name>FlexInternalServlet</servlet-name>   
  96.         <url-pattern>/flex-internal/*</url-pattern>   
  97.     </servlet-mapping>   
  98.       
<listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener> 
    
    <!-- MessageBroker Servlet -->
    <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>
        <init-param>
            <param-name>flex.write.path</param-name>
            <param-value>/WEB-INF/flex</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>FlexMxmlServlet</servlet-name>
        <display-name>MXML Processor</display-name>
        <description>Servlet wrapper for the Mxml Compiler</description>
        <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>
        <init-param>
            <param-name>servlet.class</param-name>
            <param-value>flex.webtier.server.j2ee.MxmlServlet</param-value>
        </init-param>
        <init-param>
            <param-name>webtier.configuration.file</param-name>
            <param-value>/WEB-INF/flex/flex-webtier-config.xml</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>FlexSwfServlet</servlet-name>
        <display-name>SWF Retriever</display-name>
        <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>
        <init-param>
            <param-name>servlet.class</param-name>
            <param-value>flex.webtier.server.j2ee.SwfServlet</param-value>
        </init-param>
        <!-- SwfServlet must be initialized after MxmlServlet -->
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>FlexForbiddenServlet</servlet-name>
        <display-name>Prevents access to *.as/*.swc files</display-name>
        <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>
        <init-param>
            <param-name>servlet.class</param-name>
            <param-value>flex.webtier.server.j2ee.ForbiddenServlet</param-value>
        </init-param>
    </servlet>

    <servlet>
        <servlet-name>FlexInternalServlet</servlet-name>
        <servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>
        <init-param>
            <param-name>servlet.class</param-name>
            <param-value>flex.webtier.server.j2ee.filemanager.FileManagerServlet</param-value>
        </init-param>
        <load-on-startup>10</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MessageBrokerServlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FlexMxmlServlet</servlet-name>
        <url-pattern>*.mxml</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FlexSwfServlet</servlet-name>
        <url-pattern>*.swf</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FlexForbiddenServlet</servlet-name>
        <url-pattern>*.as</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FlexForbiddenServlet</servlet-name>
        <url-pattern>*.swc</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FlexInternalServlet</servlet-name>
        <url-pattern>/flex-internal/*</url-pattern>
    </servlet-mapping>
    

    2、在spring的配置文件中增加:
   
Java代码 复制代码
  1. <bean id="topoMapViewService" parent="baseTransaction">   
  2.             <property name="target">   
  3.                     <bean   
  4.                             class="com.jeffrey.nms.service.impl.TopoServiceImpl"><!---这里的方法可以通过flex进行调用--->   
  5.                             <property name="topoMapDao" ref="topoMapDao" />   
  6.                             <property name="linkInfoDao" ref="linkInfoDao" />   
  7.                     </bean>   
  8.             </property>   
  9.     </bean>   
    <bean id="topoMapViewService" parent="baseTransaction">
                <property name="target">
                        <bean
                                class="com.jeffrey.nms.service.impl.TopoServiceImpl"><!---这里的方法可以通过flex进行调用--->
                                <property name="topoMapDao" ref="topoMapDao" />
                                <property name="linkInfoDao" ref="linkInfoDao" />
                        </bean>
                </property>
        </bean>
    

    3、然后需要配置flex的配置文件,flex文件下有个services-config.xml,
增加:
   
Java代码 复制代码
  1. <factories>   
  2.   <factory id="spring" class="com.jeffrey.nms.web.topo.SpringFactory"/>   
  3. </factories>   
    <factories>
      <factory id="spring" class="com.jeffrey.nms.web.topo.SpringFactory"/>
    </factories>
    

    其中类如下(通过网上查的)
    
Java代码 复制代码
  1.     package com.jeffrey.nms.web.topo;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.web.context.support.WebApplicationContextUtils;   
  5. import org.springframework.beans.BeansException;   
  6. import org.springframework.beans.factory.NoSuchBeanDefinitionException;   
  7.   
  8. import flex.messaging.FactoryInstance;   
  9. import flex.messaging.FlexFactory;   
  10. import flex.messaging.config.ConfigMap;   
  11. import flex.messaging.services.ServiceException;   
  12.   
  13. public class SpringFactory implements FlexFactory   
  14. {   
  15.     private static final String SOURCE = "source";   
  16.   
  17.        
  18.     public void initialize(String id, ConfigMap configMap) {}   
  19.   
  20.       
  21.     public FactoryInstance createFactoryInstance(String id, ConfigMap properties)   
  22.     {   
  23.         SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);   
  24.         instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));   
  25.         return instance;   
  26.     } // end method createFactoryInstance()   
  27.   
  28.      
  29.     public Object lookup(FactoryInstance inst)   
  30.     {   
  31.         SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;   
  32.         return factoryInstance.lookup();   
  33.     }    
  34.   
  35.   
  36.     static class SpringFactoryInstance extends FactoryInstance   
  37.     {   
  38.         SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)   
  39.         {   
  40.             super(factory, id, properties);   
  41.         }   
  42.   
  43.   
  44.         public String toString()   
  45.         {   
  46.             return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();   
  47.         }   
  48.   
  49.         public Object lookup()    
  50.         {   
  51.             ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());   
  52.             String beanName = getSource();   
  53.   
  54.             try  
  55.             {   
  56.                 return appContext.getBean(beanName);   
  57.             }   
  58.             catch (NoSuchBeanDefinitionException nexc)   
  59.             {   
  60.                 ServiceException e = new ServiceException();   
  61.                 String msg = "Spring service named '" + beanName + "' does not exist.";   
  62.                 e.setMessage(msg);   
  63.                 e.setRootCause(nexc);   
  64.                 e.setDetails(msg);   
  65.                 e.setCode("Server.Processing");   
  66.                 throw e;   
  67.             }   
  68.             catch (BeansException bexc)   
  69.             {   
  70.                 ServiceException e = new ServiceException();   
  71.                 String msg = "Unable to create Spring service named '" + beanName + "' ";   
  72.                 e.setMessage(msg);   
  73.                 e.setRootCause(bexc);   
  74.                 e.setDetails(msg);   
  75.                 e.setCode("Server.Processing");   
  76.                 throw e;   
  77.             }    
  78.         }   
  79.            
  80.     }    
  81.   
  82. }    
  83.       
    package com.jeffrey.nms.web.topo;

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;
            } 
        }
        
    } 

} 
    

    4、本人做flex时是使用的直接调用java的运程类,以下都是按照这个配置的,还有一个webservice的方法,各位可以收集收集。
     在flex文件夹下有个remoting-config.xml文件,需要增加:
    
Java代码 复制代码
  1.  <destination id="topoMap">   
  2. <properties>   
  3.     <factory>spring</factory>   
  4.     <source>topoMapViewService</source>   
  5. </properties>   
  6. lt;/destination>   
     <destination id="topoMap">
    <properties>
        <factory>spring</factory>
        <source>topoMapViewService</source>
    </properties>
   </destination>
    

     这样topoMap可以在flex进行定义了,如下:
   <mx:RemoteObject id="topo" destination="topoMap">
   这样flex+spring配置即完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值